You are not logged in.

#1 2010-02-18 18:57:44

connexion2000
Member
Registered: 2006-10-15
Posts: 116

Error in shell script

HI,
I have simple shell script:

#!/bin/sh                                 

LED_ON="echo \"1\" > /sys/class/leds/wrt160nl\:amber\:wps/brightness"
LED_OFF="echo \"0\" > /sys/class/leds/wrt160nl\:amber\:wps/brightness"

CHECK_SDA1="cat /proc/mounts | grep /dev/sda1 > /dev/null"
CHECK_SDA2="cat /proc/mounts | grep /dev/sda2 > /dev/null"
CHECK_SDA3="cat /proc/swaps | grep /dev/sda3 > /dev/null" 

MOUNT_SDA1="mount /dev/sda1 > /dev/null"
MOUNT_SDA2="mount /dev/sda2 > /dev/null"
MOUNT_SDA3="swapon /dev/sda3 > /dev/null"

UMOUNT_SDA1="umount /dev/sda1 > /dev/null"
UMOUNT_SDA2="umount /dev/sda2 > /dev/null"
UMOUNT_SDA3="swapoff /dev/sda3 > /dev/null"

if [ eval $CHECK_SDA1 -eq 0 ] || [ eval $CHECK_SDA2 -eq 0 ] || [ eval $CHECK_SDA3 -eq 0 ]; then
    if [ eval $UMOUNT_SDA1 -eq 0 ] && [ eval $UMOUNT_SDA2 -eq 0 ] && [ eval $UMOUNT_SDA3 -eq 0 ]; then
        eval $LED_OFF
    fi
else
    if [ eval $MOUNT_SDA1 -eq 0 ] || [ eval $MOUNT_SDA2 -eq 0 ] || [ eval $MOUNT_SDA3 -eq 0 ]; then
        eval $LED_ON
    fi
fi

but it return errors:

sh: cat: unknown operand
sh: cat: unknown operand
sh: cat: unknown operand
sh: mount: unknown operand
sh: mount: unknown operand
sh: swapon: unknown operand

Does anybody knows what is wrong?

Last edited by connexion2000 (2010-02-18 18:57:56)

Offline

#2 2010-02-18 19:35:04

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: Error in shell script

I don't really getting why you are trying all that eval stuff there - why don't you just test the command you want?

if grep -q '/dev/sda1' /proc/mounts || ...

And evaluating SOMECMD doesn't yield it's return value but it's output - so testing with test and -eq won't work here.

Offline

#3 2010-02-18 20:33:04

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: Error in shell script

Try it without the square brackets, for example

if eval $CHECK_SDA1 || eval $CHECK_SDA2 || eval $CHECK_SDA3; then
...

The reason why your version didn't work is that the "if" statement takes a command and runs it, deciding "true" if the command returns 0 and "false" if it returns anything else. The square brackets look like special shell syntax, but they are actually a command. Normally [ is a shell builtin, but you can use the "test" command instead which will yield the same results. The ] character is really just a mandatory final argument to the [ command, which explains why it has to be surrounded by spaces.

This whole thing confused me for ages until I figured out what was going on. IMHO they shouldn't have tried to disguise it by making it look like some special syntax, but there you are, that's how it is.

Also it is a bit strange to put your commands in variables like that. For the longer ones you could put them into a function as explained here: http://www.tech-recipes.com/rx/541/bour … functions/
Functions will also help eliminate repetition, but you don't need to worry too much about that sort of thing in a simple script.

Offline

#4 2010-02-18 20:42:23

connexion2000
Member
Registered: 2006-10-15
Posts: 116

Re: Error in shell script

Bralkein wrote:

Try it without the square brackets, for example

if eval $CHECK_SDA1 || eval $CHECK_SDA2 || eval $CHECK_SDA3; then
...

The reason why your version didn't work is that the "if" statement takes a command and runs it, deciding "true" if the command returns 0 and "false" if it returns anything else. The square brackets look like special shell syntax, but they are actually a command. Normally [ is a shell builtin, but you can use the "test" command instead which will yield the same results. The ] character is really just a mandatory final argument to the [ command, which explains why it has to be surrounded by spaces.

This whole thing confused me for ages until I figured out what was going on. IMHO they shouldn't have tried to disguise it by making it look like some special syntax, but there you are, that's how it is.

Also it is a bit strange to put your commands in variables like that. For the longer ones you could put them into a function as explained here: http://www.tech-recipes.com/rx/541/bour … functions/
Functions will also help eliminate repetition, but you don't need to worry too much about that sort of thing in a simple script.

NIICEEEEE, thanks.
But also there is one more problem. Only $CHECK_SDA1 is executed and if it is true, $CHECK_SDA2 isn't being executed.
How to execute all these commands?

Offline

#5 2010-02-18 20:53:58

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: Error in shell script

you're using a logical OR. 

$one || $two || $three will return true if any one of them is true; therefore, why would the parser ever go passed the first check if it's true?  there's simply no need.

also

CHECK_SDA1="cat /proc/mounts | grep /dev/sda1 > /dev/null"

# could really be

check_sda1() { grep -Fq '/dev/sda1' /proc/mounts; }

# then you can more easily

if $check_sda1; then 
  echo 'zomg sda1 is mounted!'
fi

# but

check() { grep -Fq "$1" /proc/mounts; }

# would be a bit smarter, i'll let you figure out why on your own tho ;)

Offline

Board footer

Powered by FluxBB