LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Syntax Problem (https://www.linuxquestions.org/questions/linux-newbie-8/bash-syntax-problem-852430/)

calciferfelix 12-26-2010 05:13 AM

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

druuna 12-26-2010 05:21 AM

Hi,

Spacing is important: if [ "$?" == 0 ] instead of if["$?"==0].

Hope this helps.

smoker 12-26-2010 05:26 AM

You need spaces between comparison operators and operands.
Also, the semi-colon denotes the end of a statement. You either need the then on the same line or remove the semi-colon.

Code:

if ["$?" == 0]; then
    echo 1
else
    echo 0
fi

or
Code:

if ["$?" == 0]
then
    echo 1
else
    echo 0
fi

There are also interesting differences between using double or single square brackets around the test condition.

http://tldp.org/LDP/Bash-Beginners-G...ect_07_01.html

calciferfelix 12-26-2010 05:40 AM

Thank you very much, both of you, it worked.

druuna 12-26-2010 05:43 AM

Hi,

This works (remember: s p a c i n g):
Code:

if [ "$?" == 0 ]
then
    echo 1
else
    echo 0
fi

If that does not work: Please post the script you are working on, and not just the part you think fails.

Hope this helps.

calciferfelix 12-26-2010 05:47 AM

I misunderstand and just use space in the square brackets, but not after if. But it works now, thank you for patience :)

druuna 12-26-2010 05:49 AM

You're welcome :)


All times are GMT -5. The time now is 11:15 PM.