LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   help with basic syntax in bash script (https://www.linuxquestions.org/questions/linux-software-2/help-with-basic-syntax-in-bash-script-52054/)

Supp0rtLinux 03-27-2003 02:14 PM

help with basic syntax in bash script
 
I am working on an external script for BB (bb4.com) and hitting a sort of impasse... the beginning of my script works fine... it checks a few things and if all is good, does nothing... later 'nothing' will equal green. There are a few things that can error out and if any of them do, it simply touches a file relating to the error. Later, my script tests for the existence of the files... the existence of certain files sets yellow or red, the absence of all files sets COLOR=green.

Here's my problem... if I try the following code:

for i in $BBTMP/array.*
do
if [ -f array.offline ]
then
COLOR=red
elsif [ -f array.unbound ]
then
COLOR=yellow
elsif [ -f array.unknown ]
then
COLOR=yellow
else
COLOR=green
fi
done

...when I run it, I get:
./bb-script.sh: line 24: syntax error near unexpected token `then'
./bb-script.sh: line 24: ` then'

Line #24 is the 2nd "then" listed.

--------------------

So as a workaround, I tried this:

if [ -f $BBTMP/array.offline ]
then
COLOR="red"
elsif [ -f $BBTMP/array.unbound ]
then
COLOR="yellow"
elsif [ -f $BBTMP/array.unknown ]
then
COLOR="yellow"
elsif [ ! -f $BBTMP/array.* ]
then
COLOR="green"
fi

This takes out the whole "for i..." stuff but still does the same test.

Now I get:
./bb-script.sh: line 38: syntax error near unexpected token `then'
./bb-script.sh: line 38: `then'

Line #38 now correlates to the 2nd "then" just as before.

So then I tried:

for i in $BBTMP/array.*
do
if [ -f array.offline ]
then
COLOR="red"
elsif [ -f array.unbound ]
COLOR="yellow"
elsif [ -f array.unknown ]
COLOR="yellow"
else
COLOR="green"
fi
done

Basically... I removed all the offending "then"s. Now it doesn't error out, but it also doesn't work. $COLOR=green despite the fact that both $BBTMP/array.offline and $BBTMP/array.unknown exist on this system.

The only way I've been able to make this work is to remove all the 'elsif's and instead do each test with a beginning 'if' and a closing 'fi'. The code looks like this:

for i in $BBTMP/array.*
do
if [ -f array.offline ]
then
COLOR="red"
fi
if [ -f array.unbound ]
then
COLOR="yellow"
fi
if [ -f array.unknown ]
then
COLOR="yellow"
fi
else
COLOR="green"
fi
done

This actually works and gets the right color if only one or none of the array.* files exists, but it poses a problem... if both array.offline and array.unknown exist, then it goes yellow since that was the last test done... but since both exist, it should be red... the elsif accounts for this... the individual if... fi's do not.

I've searched all over google. I've checked some of the online bash docs... specifically http://pegasus.rutgers.edu/~elflord/unix/bash-tute.html & http://www.scit.wlv.ac.uk/~jphb/spos...ll/shell5.html

The closest I came to finding a resolution on google was a reference to having an older bash, but I already have the latest bash. I've even tried setting my first line to #!/bin/sh, but I get the same results. Env is RH7.3 x86. I recognize that I have a syntax error, but I can't find it. Maybe I've been up too late tonite. Can any of you offer any input? I'm really confused here. Based on the bash man pages, O'Reilly books, and other links on Google that I've read... I think my syntax is correct, I just can't figure out what it doesn't seem to like.

Mara 03-27-2003 02:34 PM

Maybe an ';' after if? Something like
if [something]; then something_else;elif something_else2; then something_else3; fi

Supp0rtLinux 03-27-2003 05:07 PM

tired that... didn't make a difference... the error was the same, so I know the ; is permitted, but it didn't resolve the problem :(

cuckoopint 03-27-2003 06:43 PM

bash(1):

Quote:

if list; then list; [ elif list; then list; ] ... [ else list; ] fi
i think you can figure out your error from that....

cheers

Supp0rtLinux 03-27-2003 06:57 PM

a few more semi-colons and elif instead of elsif

thanks... all works now. Hukt on fonix werkt fer me

>heh, heh... he said semi<


All times are GMT -5. The time now is 07:59 PM.