LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 05: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, 05:21 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

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

Rep: Reputation: 250Reputation: 250Reputation: 250
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 05:29 AM. Reason: clarification
 
Old 12-26-2010, 05: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 05:44 AM.
 
Old 12-26-2010, 05:43 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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, 05: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, 05:49 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:30 AM.

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
Open Source Consulting | Domain Registration