Find the answer to your Linux question:
Results 1 to 2 of 2
hey guys, I'm new to scripting and I have a trouble with if statement syntax. The code is: Code: #there is a diff command here, and it does what i ...
  1. #1
    Just Joined!
    Join Date
    Dec 2010
    Posts
    1

    Bash Syntax Problem

    hey guys, I'm new to scripting and I have a trouble with if statement syntax.

    The code is:

    Code:
    #there is a diff command here, and it does what i want but
    #i wanna see 1 if the exit value of diff is 0, and otherwise i wanna see 0.
    #the problem is here: (syntax error near unexpected token "then")
    
    if["$?"==0];
    then
    echo 1
    else
    echo 0
    fi
    so what could be the problem?
    thanks

  2. #2
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    121
    Quote Originally Posted by calciferfelix View Post
    Code:
    #the problem is here: (syntax error near unexpected token "then")
    
    if["$?"==0];
    then
    echo 1
    else
    echo 0
    fi
    I think you'll also find you get the following error (assuming you've cut & pasted correctly from your source, and if you don't do that then there's really no way anybody can help you...):

    Code:
    ... if[0==0]: command not found
    At the very least, you need spaces either side of the `[' and in front of the `]'. Normally, that would be spaced like this for readability:

    Code:
    if [ "$?" == 0 ];
    Remember to space things correctly (especially with bash which, as you can see, relies on spaces in some cases to separate tokens) and to always try and fix the first (very very first) error thrown at you. Anything else is a waste of time.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...