5 Replies - 6975 Views - Last Post: 31 July 2008 - 09:48 AM Rate Topic: -----

#1 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

bash if statement

Posted 19 June 2008 - 12:46 AM

am having problems with my else if statement in bash... it should be going into the elif but it drops down to the else and when it runs its doing something funky

Quote

DEBUG-|100|
bash: [: 100: unary operator expected
bash: [100: command not found
DEBUG-else


echo "DEBUG-|$CURR_STATUS|"	
#server isnt in db
if [ $CURR_STATUS -z ]; then
	echo "DEBUG-in no status set"
	# new server 
	CURR_STATUS=100
elif [$CURR_STATUS -eq 100]; then
	echo "DEBUG-in status 100"
else
	echo "DEBUG-else"
fi


Is This A Good Question/Topic? 0
  • +

Replies To: bash if statement

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5345
  • View blog
  • Posts: 27,296
  • Joined: 10-May 07

Re: bash if statement

Posted 19 June 2008 - 02:01 AM

There are 2 slight syntax errors in your code.

1st, the dash z must come before the variable it's testing on line 4. I don't know why, that's just the way it is.

2nd, you need spaces between your open bracket & close bracket, on line 8.

echo "DEBUG-|$CURR_STATUS|"
#server isnt in db
if [ -z $CURR_STATUS ]; then
	echo "DEBUG-in no status set"
	# new server
	CURR_STATUS=100
elif [ $CURR_STATUS -eq 100 ]; then
	echo "DEBUG-in status 100"
else
	echo "DEBUG-else"
fi


Was This Post Helpful? 0
  • +
  • -

#3 realNoName  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 343
  • Joined: 04-December 06

Re: bash if statement

Posted 19 June 2008 - 02:36 AM

sweet thanks no2pencil


p.s. i HATE bash scripting
Was This Post Helpful? 0
  • +
  • -

#4 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: bash if statement

Posted 19 June 2008 - 09:55 PM

Quote

p.s. i HATE bash scripting


I don't mind it, but I use PHP for scripts whenever I can get away with it. :)

This post has been edited by Tom9729: 19 June 2008 - 09:56 PM

Was This Post Helpful? 0
  • +
  • -

#5 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: bash if statement

Posted 13 July 2008 - 08:24 AM

Well, I think the reasoning behind the syntax is easy once one understands the underlying mechanisms here. You see, the [ and ] are not part of the shell language (sometimes they are, but they have to be compatible with older shells). The [ in the if compound statement is really either /bin/[ (a symlink to /bin/test), or is a built in. So, "if [ -z $X ] ; then" should be similar to "if test -z $x ; then". The test binary will ignore the "]" purposely, just to support this functionality. The if statement simply executes whatever comes after it, and evaluates the exit code in order to decide what block to execute thereafter.

BTW, some shells offer a [[ builtin as well, that usually supports a superset of what can be given to the [ external/builtin.
Was This Post Helpful? 0
  • +
  • -

#6 indrora  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 100
  • Joined: 25-July 08

Re: bash if statement

Posted 31 July 2008 - 09:48 AM

take a look at this (http://sonof.bandit.name/index.php?entry=entry080305-172922#readmore-entry080305-172922 )for a good usage of if's
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1