[Gllug] Simple Bash Script

Iain Gray iain.dv at googlemail.com
Mon Jun 2 09:45:40 UTC 2008


if [ "$RESULT == $TRUE" ];
Isn't BASH silly in that it uses == for string comparison and -eq for integer?

So this line should be written

if [ $RESULT -eq $TRUE ]

2008/6/2 william pink <will.pink at gmail.com>:
> Hello All,
>
> I have a script that checks for system load then sends a email if it is over
> a certain threshold ( Its a cut down version from the one on Nixcraft )
> Here is the script
>
>
> #!/bin/bash
>
> # Script to check system load and remove from cluster if higher than 6.0
>
> TEMPFILE="$(mktemp)"
> NOTIFY="6.0"
> HOST=`hostname`
> RCLUSTER=`/usr/local/bin/in-cluster no`
> #load at 1 minute
> TRUE="1"
> LOAD=`uptime | sed -e "s/.*load average: \(.*\...\), \(.*\...\),
> \(.*\...\)/\1/" -e "s/ //g"`
>
> #input in tempfile
> echo "Load average to high removed from cluster, Load currently at $LOAD
> allowed limit $NOTIFY." > $TEMPFILE
>
> # Compare results to benchmark
> RESULT=$(echo "$LOAD > $NOTIFY" | bc)
>
> # IF so do your thing
>
> if [ "$RESULT == $TRUE" ]; then
>         $RCLUSTER
>         mail -s "$HOST has been removed from the cluster" "$EMAIL" <
> $TEMPFILE
> fi
>
> rm -f $TEMPFILE
>
> What I am having problems is bc will return back the value 0 but it will
> still execute the commands
>
> here is the bash -x output
>
> root at thebrain:/home/will/scripts# bash -x loadtest.sh
> + bash -x loadtest.sh
> ++ mktemp
> + TEMPFILE=/tmp/tmp.xtzco30283
> + NOTIFY=3
> ++ hostname
> + HOST=thebrain
> + TRUE=1
> ++ uptime
> ++ sed -e 's/.*load average: \(.*\...\), \(.*\...\), \(.*\...\)/\1/' -e 's/
> //g'
> + LOAD=2.19
> + echo 'Load average to high removed from cluster, Load currently at 2.19
> allowed limit 3.'
> ++ echo '2.19 >= 3'
> ++ bc
> + RESULT=0
> + '[' '0 == 1' ']'
> + echo test then
> test then
> + echo 'test failed!'
> test failed!
> + rm -f /tmp/tmp.xtzco30283
> ++ echo -ne '\033]0;root at thebrain: /home/will/scripts\007'
>
> Any suggestions gratefully received
>
> Cheers,
> Will
>
> P.S Of course a easier way to do it would be for it to just check the RESULT
> variable and see if it is a 1 or 0 and just act upon that but I would like
> to get this method working.
>
> --
> Gllug mailing list  -  Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug
>
>
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list