LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
LinkBack Search this Thread
Old 04-18-2007, 04:06 AM   #1
sureshkumar.repaka
LQ Newbie
 
Registered: Apr 2007
Posts: 5

Rep: Reputation: 0
syntax error near unexpected token `fi'


I trying to debug as below code

#!/bin/bash
STR="Hello World!"
if [$STR= = "Hello World!"]; then
echo "Goodbye"
fi


But shown error is "syntax error near unexpected token 'fi' "

Please tell any solution on this error
 
Old 04-18-2007, 04:29 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

You code has a few errors.

Here's a working version:
Code:
#!/bin/bash
STR="Hello World!"
if [[ $STR == "Hello World!" ]]; then
echo "Goodbye"
fi
Mind the space between [ and $STR and !" and ]
You also need double square brackets (open and close).

Hope this helps.
 
Old 04-18-2007, 05:00 AM   #3
sureshkumar.repaka
LQ Newbie
 
Registered: Apr 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Hi
Thanks.But again shown same error when debugging as mentioned code snnipet.
 
Old 04-18-2007, 05:24 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371Reputation: 2371
Hi,

I tried the code on a unix box and it works like it should:

$ cat tstfile

#!/bin/bash
STR="Hello World!"
if [[ $STR == "Hello World!" ]]; then
echo "Goodbye"
fi

$ ./tstfile
Goodbye

Only thing I can come up with is the use of the ! in your string. ! is kinda special and escaping it (\!) will probably not work.
Using single quotes should help. I.e:
Code:
#!/bin/bash
STR='Hello World!'
if [[ $STR == 'Hello World!' ]]; then
echo "Goodbye"
fi
Hope this helps.
 
Old 04-18-2007, 05:37 AM   #5
sureshkumar.repaka
LQ Newbie
 
Registered: Apr 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Hi,
O.s is Fedora Core release 3 (Heidelberg) and shown
error like
[root@nagios checkservices]# bash firstservice.sh
firstservice.sh: line 5: syntax error near unexpected token `fi'
firstservice.sh: line 5: `fi'
[root@nagios checkservices]#
 
Old 04-18-2007, 05:54 AM   #6
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 52
Did you copy past the last script from druuna?
It works on my machine.
 
Old 04-19-2007, 06:28 AM   #7
dasy2k1
Member
 
Registered: Oct 2005
Location: 127.0.0.1
Distribution: Ubuntu 12.04 X86_64
Posts: 956

Rep: Reputation: 34
I have had teh same error countless times especially on cygwin and the answer lies in the line endings...

check that your code has Unix line endings (\n)

not windows/dos line endings \r\n as the \r is interpeted as an unexpected token.

if you are writing this on a wdoze machine i recomend using Jedit then using teh buffer options to select Unix line endings.

Last edited by dasy2k1; 04-19-2007 at 06:30 AM.
 
Old 08-19-2007, 10:14 PM   #8
crossley
LQ Newbie
 
Registered: Feb 2003
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by dasy2k1 View Post
I have had teh same error countless times especially on cygwin and the answer lies in the line endings...

check that your code has Unix line endings (\n)

not windows/dos line endings \r\n as the \r is interpeted as an unexpected token.

if you are writing this on a wdoze machine i recomend using Jedit then using teh buffer options to select Unix line endings.
Oh boy!!! thx!!! This was exactly my problem!!!!, damn Windows!!!
 
  


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 On
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error near unexpected token `fi' Warmduvet Programming 21 10-08-2013 10:28 AM
compiling error: unexpected token `fi' rem1986 Linux - Kernel 7 12-10-2007 01:11 PM
syntax error near unexpected token `else' josedias Programming 3 09-11-2006 07:09 PM
Trouble with Bash -- syntax error near unexpected token `fi' anamericanjoe Programming 5 05-19-2006 02:59 PM
syntax error near unexpected token ` mattyspatty Programming 8 05-07-2006 05:19 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration