LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-27-2003, 02:14 PM   #1
Supp0rtLinux
Member
 
Registered: Dec 2001
Location: Carlsbad
Distribution: LFS 4.0 (www.linuxfromscratch.org)
Posts: 44

Rep: Reputation: 15
Question 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.
 
Old 03-27-2003, 02:34 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Maybe an ';' after if? Something like
if [something]; then something_else;elif something_else2; then something_else3; fi
 
Old 03-27-2003, 05:07 PM   #3
Supp0rtLinux
Member
 
Registered: Dec 2001
Location: Carlsbad
Distribution: LFS 4.0 (www.linuxfromscratch.org)
Posts: 44

Original Poster
Rep: Reputation: 15
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
 
Old 03-27-2003, 06:43 PM   #4
cuckoopint
Member
 
Registered: Feb 2003
Distribution: Debian
Posts: 797

Rep: Reputation: 30
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
 
Old 03-27-2003, 06:57 PM   #5
Supp0rtLinux
Member
 
Registered: Dec 2001
Location: Carlsbad
Distribution: LFS 4.0 (www.linuxfromscratch.org)
Posts: 44

Original Poster
Rep: Reputation: 15
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<
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic bash script question kevpatts Linux - Newbie 3 10-12-2005 01:02 PM
Basic Bash script help valadamir Programming 2 12-01-2004 10:37 AM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 09:09 AM
Trouble with a basic bash script Tengil Linux - Newbie 5 03-04-2004 12:59 PM
Basic BASH script, what's wrong??? Satriani Linux - General 2 06-02-2003 05:34 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration