Reply
Thread Tools
santiago's Avatar
Posts: 518 | Thanked: 334 times | Joined on Mar 2010 @ italy
#11
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 i could cut the way and the time

Last edited by santiago; 2011-11-20 at 13:50.
 
santiago's Avatar
Posts: 518 | Thanked: 334 times | Joined on Mar 2010 @ italy
#12
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...
 
Posts: 99 | Thanked: 65 times | Joined on Jan 2008 @ Finland
#13
Originally Posted by santiago View Post
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

Last edited by wnd; 2011-11-22 at 10:02. Reason: Backticks
 

The Following User Says Thank You to wnd For This Useful Post:
Reply

Thread Tools

 
Forum Jump


All times are GMT. The time now is 12:54.