Welcome to Dream.In.Code
Getting Help is Easy!

Join 132,474 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,429 people online right now. Registration is fast and FREE... Join Now!




bash if statement

 
Reply to this topicStart new topic

bash if statement

realNoName
post 18 Jun, 2008 - 11:46 PM
Post #1


D.I.C Regular

***
Joined: 4 Dec, 2006
Posts: 299



Thanked 5 times
My Contributions


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


CODE
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
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 19 Jun, 2008 - 01:01 AM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,328



Thanked 57 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


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.

CODE

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
User is online!Profile CardPM

Go to the top of the page

realNoName
post 19 Jun, 2008 - 01:36 AM
Post #3


D.I.C Regular

***
Joined: 4 Dec, 2006
Posts: 299



Thanked 5 times
My Contributions


sweet thanks no2pencil


p.s. i HATE bash scripting
User is offlineProfile CardPM

Go to the top of the page

Tom9729
post 19 Jun, 2008 - 08:55 PM
Post #4


Debian guru

Group Icon
Joined: 30 Dec, 2007
Posts: 1,447



Thanked 10 times

Dream Kudos: 325
My Contributions


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. smile.gif

This post has been edited by Tom9729: 19 Jun, 2008 - 08:56 PM
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 13 Jul, 2008 - 07:24 AM
Post #5


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

indrora
post 31 Jul, 2008 - 08:48 AM
Post #6


D.I.C Head

Group Icon
Joined: 25 Jul, 2008
Posts: 52



Dream Kudos: 25
My Contributions


take a look at this (http://sonof.bandit.name/index.php?entry=entry080305-172922#readmore-entry080305-172922 )for a good usage of if's
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 02:19PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month