maemo.org - Talk

maemo.org - Talk (https://talk.maemo.org/index.php)
-   Maemo 5 / Fremantle (https://talk.maemo.org/forumdisplay.php?f=40)
-   -   Help - Bash script - SOLVED you can close this thread ;) a new package is coming for us (https://talk.maemo.org/showthread.php?t=80152)

santiago 2011-11-20 08:45

Help - Bash script - SOLVED you can close this thread ;) a new package is coming for us
 
hi guys, i'm working with bash, i'm creating a script that check the battery status and the wall carger status to do some things like keep battery life disabling some features (wifi, 3g mode, ringtone, vibration, bluetooth, fmradio, etc)

can somone help me? the script has to save the current profile if different from silent, i made some mistakes and i'm a little bit confused with it becouse it doesnt save the right used profile.

this is the code i made

**** the code is in test for that i left "battery.charge_level.percentage = " empty to test it better

bingomion 2011-11-20 11:21

Re: Help - Bash script
 
good script :)
I googled, found this:
http://talk.maemo.org/showthread.php?t=49904
hope it helps

santiago 2011-11-20 11:42

Re: Help - Bash script
 
Quote:

Originally Posted by bingomion (Post 1126293)
good script :)
I googled, found this:
http://talk.maemo.org/showthread.php?t=49904
hope it helps

it's yet in the code, the problem is not this, but the check code and the restore code about the used profile ;)

Zas 2011-11-20 12:17

Re: Help - Bash script
 
You could try "cp /home/user/.profiled/current /home/user/profilo.txt"

santiago 2011-11-20 12:24

Re: Help - Bash script
 
Quote:

Originally Posted by Zas (Post 1126307)
You could try "cp /home/user/.profiled/current /home/user/profilo.txt"

i'm using this line

Code:

        run-standalone.sh dbus-send --type=method_call --print-reply --dest=com.nokia.profiled /com/nokia/profiled com.nokia.profiled.get_profile | grep "string" | sed s/'  string '/''/g > /home/user/profilo.txt && sleep 2
becouse i have to remove the "string" output line and keep just the profile in the quotations.. The problem that i have is that the script does everythin i wrote, but it doesnt load the right profile couse i cant find the error..

santiago 2011-11-20 12:29

Re: Help - Bash script
 
Quote:

Originally Posted by Zas (Post 1126307)
You could try "cp /home/user/.profiled/current /home/user/profilo.txt"

Oh my god u saved my life thx!!!!!!!! u made my day!!!!!
i was doing a stupid work :D it was not needed

joerg_rw 2011-11-20 12:33

Re: Help - Bash script
 
For God's sake please use functions () { }; in your script
And attach scripts here to tmo as attachment, rather than using pastebin which will vanisch in a week, or month, or year the latest

BTW if you definitely need bash then you should use
#!/bin/bash
Your shebang is calling ash aka messybox on a standard maemo system

/j

santiago 2011-11-20 12:37

Re: Help - Bash script
 
Quote:

Originally Posted by joerg_rw (Post 1126316)
For God's sake please use functions () { }; in your script
And attach scripts here to tmo as attachment, rather than using pastebin which will vanisch in a week, or month, or year the latest

BTW if you definitely need bash then you should use
#!/bin/bash
Your shebang is calling ash aka messybox on a standard maemo system

/j

i tried using functions but i was worse for me :)

joerg_rw 2011-11-20 12:46

Re: Help - Bash script
 
Quote:

Originally Posted by santiago (Post 1126320)
i tried using functions but i was worse for me :)

where's the problem?

If you got 2 or more locations that look like

Code:

...
A
B
C
D
...

then you get *one*

Code:

myABCDfunction()
{
  A
  B
  C
  D
}

and replace each occurrence of the four lines A B C D by
Code:

...
myABCDfunction
...




If you got
Code:

B 1
at one location and
Code:

B 2
at another, then do
Code:

myABCDfunction()
{
  A
  B $1
  C
  D
}

and call like
Code:

...
myABCDfunction 1
...
myABCDfunction 2
...


[edit]
another comment: when you need to use sudo then you obviously don't run the script under root, so you don't need to use run-standalone (though it doesn't hurt).
To do arbitrary commands via sudo you probably need to tweak sudoers config in /etc/sudores.d/yourfile a bit.
[/edit]

santiago 2011-11-20 12:49

Re: Help - Bash script
 
Quote:

Originally Posted by joerg_rw (Post 1126322)
where's the problem?

If you got 2 or more locations that look like

Code:

...
A
B
C
D
...

then you get *one*

Code:

myABCDfunction()
{
  A
  B
  C
  D
}

and replace each occurrence of the four lines A B C D by
Code:

...
myABCDfunction
...

If you got
Code:

B 1
at one location and
Code:

B 2
at another, then do
Code:

myABCDfunction()
{
  A
  B $1
  C
  D
}

and call like
Code:

...
myABCDfunction 1
...
myABCDfunction 2
...


i did it :D i didnt rest for one night i was linea vampire ... now i try :) and i'll add a code here in a zip file

santiago 2011-11-20 13:07

Re: Help - Bash script
 
right like this ? i attached the file

i can use this to compare

check='cat /home/user/.profiled/current'
if [ $profilo = $check ]: then
......


i did a long work :D i could cut the way and the time :D

santiago 2011-11-20 14:07

Re: Help - Bash script
 
ok maybe the script works, how can i compare functions with if then else?

i wrote

if 1rstcheck then
echo "1rst check exists" else
2ndcheck then
echo "2nd check exists" else
3rdcheck then
echo "3rd check exists"


but it doesnt work...

wnd 2011-11-22 10:00

Re: Help - Bash script
 
Quote:

Originally Posted by santiago (Post 1126355)
ok maybe the script works, how can i compare functions with if then else?

i wrote

if 1rstcheck then
echo "1rst check exists" else
2ndcheck then
echo "2nd check exists" else
3rdcheck then
echo "3rd check exists"


but it doesnt work...

I'm not sure if I follow. Maybe you're after this:

Code:

if condition1; then
    foo1
elif condition2; then
    foo2
else
    foox
fi

Also consider writing program logic in "body" of the script, and do the magic in functions. This is what I mean:

Code:

#! /bin/sh

set -e

sub phone_is_charging() {
    lshal -u /org/freedesktop/Hal/devices/bme | grep -iq "maemo.rechargeable.charging_status = 'on'"
}

sub set_profile_and_foo() {
    foo $1
}


# main logic starts here

if phone_is_charging; then
    set_profile_and_foo General
else
    set_profile_and_foo Silent
fi


If you end up using functions, bash provides scope for variables (unlike standard Bourne shell).

Code:

#! /bin/bash

sub foo() {
    local x="$1"
    echo $x
}

x=xyzzy
foo bar
echo $x

As for backticks, please don't use them. This is 21st century.
Code:

foo="$(cat "$filename")"

And finally, you may want to proect your script against unset variables.

Code:

test "$variable" = "yes"
test "x$variable" = "xyes"  # if you're paranoid



All times are GMT. The time now is 20:13.

vBulletin® Version 3.8.8