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 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