LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-26-2010, 06:13 AM   #1
calciferfelix
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Rep: Reputation: 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
 
Old 12-26-2010, 06:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

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

Hope this helps.
 
Old 12-26-2010, 06:26 AM   #3
smoker
Senior Member
 
Registered: Oct 2004
Distribution: Fedora Core 4, 12, 13, 14, 15, 17
Posts: 2,279

Rep: Reputation: 249Reputation: 249Reputation: 249
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

Last edited by smoker; 12-26-2010 at 06:29 AM. Reason: clarification
 
Old 12-26-2010, 06:40 AM   #4
calciferfelix
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 1
Thank you very much, both of you, it worked.

Last edited by calciferfelix; 12-26-2010 at 06:44 AM.
 
Old 12-26-2010, 06:43 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
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.
 
Old 12-26-2010, 06:47 AM   #6
calciferfelix
LQ Newbie
 
Registered: Dec 2010
Posts: 7

Original Poster
Rep: Reputation: 1
I misunderstand and just use space in the square brackets, but not after if. But it works now, thank you for patience
 
Old 12-26-2010, 06:49 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
You're welcome
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
bash: syntax error denneyd Linux - Software 3 04-08-2010 09:41 PM
bash, syntax error santana Linux - Software 2 01-23-2008 02:39 PM
SED in BASH Script - Syntax Problem stlouis Programming 4 01-13-2008 11:38 AM
bash syntax Sushy Programming 1 06-04-2007 07:25 PM
BASH : syntax error semaja2 Programming 6 01-26-2006 01:33 AM


All times are GMT -5. The time now is 06:24 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration