LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-29-2010, 10:24 AM   #1
roxie600
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Rep: Reputation: 0
Shell Scripting "syntax error : unexpected end of file"


hey
I am very new to shell scripting and keep getting this error message so any advice would be gladly received!

I have simplified my code majorly and still get the same error so there's obviously something wrong with my basic syntax.

#usr/bin/sh

if [$1 -lt $2]; then
echo "done"
else
echo "nope"
fi

Thanks
 
Old 04-29-2010, 10:35 AM   #2
deadeyes
Member
 
Registered: Aug 2006
Posts: 603

Rep: Reputation: 78
Quote:
Originally Posted by roxie600 View Post
hey
I am very new to shell scripting and keep getting this error message so any advice would be gladly received!

I have simplified my code majorly and still get the same error so there's obviously something wrong with my basic syntax.

#usr/bin/sh

if [$1 -lt $2]; then
echo "done"
else
echo "nope"
fi

Thanks
Code:
#usr/bin/sh

if [ $1 -lt $2 ]; then
echo "done"
else
echo "nope"
fi
Can you copy paste this?
Normally you need spaces between the if and brackets, ...
 
Old 04-29-2010, 11:03 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
BTW, if you are very new to shell scripting you might like to start off with the habit of using [[ ]] rather than [ ] for reasons explained here.
 
Old 04-29-2010, 11:20 AM   #4
roxie600
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
hey
i copied and pasted your code and i still got the same error - i also tried using [[ but still got the same error
 
Old 04-29-2010, 11:44 AM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Works for me.

Try runnning 'dos2unix /path/to/your/script' and try the script again.

Dave
 
Old 04-29-2010, 12:30 PM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,512
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
The #usr/bin/sh is strange. Try changing it to #!/bin/bash
 
Old 04-29-2010, 12:43 PM   #7
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Please try this:
Code:
#!/bin/sh

if [ $# -lt 2 ]; then
  echo 'ERROR: please enter two numbers!'
  exit
fi

if [ $1 -lt $2 ]; then
  echo "$1 is less than $2..."
else
  echo "$2 is less than or equal to $1..."
fi
A couple of notes:
* On most Linux systems, "/bin/sh" is an alias to "bash"
* For portability, I used "sh" syntax (instead of bash extensions)
* I checked to make sure we had (at least) two arguments, using "$#"
* When using "[ ]" syntax, you need spaces before and after the brackets
* The reason is that "[ ]" is itself an alias (an alias for the shell "test" command)
* Always use "code blocks" (the little "#" icon on your LQ toolbar)

'Hope that helps .. PSM

Last edited by paulsm4; 04-29-2010 at 12:47 PM.
 
Old 04-29-2010, 02:19 PM   #8
roxie600
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
hi
PSM - when I ran your code I got an error message saying 'fi' unexpected on line 12
Dave - when I ran the dos2unix command nothing changed either

Thanks for the help btw
 
Old 04-29-2010, 02:42 PM   #9
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
when I ran your code I got an error message saying 'fi' unexpected on line 12
'Hate to say it - but it sounds like you didn't cut/paste correctly

Please try again and see what happens.
 
Old 04-29-2010, 02:58 PM   #10
roxie600
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Am still getting the end of file unexpected error (it saved in the wrong place before so was still running the old one)
 
Old 04-29-2010, 04:49 PM   #11
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
"syntax error : unexpected end of file"
<= this is one thing

Quote:
fi' unexpected on line 12
<= This is a completely different thing

Quote:
cd /tmp

vi tmp.sh
Code:
#!/bin/sh

if [ $# -lt 2 ]; then
  echo 'ERROR: please enter two numbers!'
  exit
fi

if [ $1 -lt $2 ]; then
  echo "$1 is less than $2..."
else
  echo "$2 is less than or equal to $1..."
fi
chmod +rx tmp.sh

ls -l tmp.sh
-rwxr-xr-x 1 root root 192 Apr 29 14:48 tmp.sh

./tmp.sh
ERROR: please enter two numbers!

./tmp.sh 1 2
1 is less than 2...
Please follow the above steps EXACTLY:
1. cd to your "/tmp" directory

2. Create a file called "tmp.sh"

3. Cut and paste the contents of this file EXACTLY

4. Set "execute" permissions (by running "chmod +rx tmp.sh")

5. Run the file ("./tmp.sh") with no arguments

6. Run the file with two numeric arguments ("./tmp.sh 1 2", or "./tmp.sh 10 9")

7. Post back the results

8. When you post back, please be sure to cut/paste YOUR version of the shell script that you executed.
Use code blocks (the "#" symbol on the LQ toolbar).

Thank you in advance .. PSM

Last edited by paulsm4; 04-29-2010 at 04:55 PM.
 
Old 04-29-2010, 05:30 PM   #12
roxie600
LQ Newbie
 
Registered: Apr 2010
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you so much! That's now working fine. I will continue working that way from now on.
Thanks again for your help
 
Old 04-30-2010, 12:18 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,169

Rep: Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777Reputation: 1777
Please mark as SOLVED
 
0 members found this post helpful.
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
syntax error: unexpected end of file ygdrazil Linux - Newbie 3 07-23-2009 05:00 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 06:31 AM
Backup Script error "line 31: syntax error: unexpected end of file" eswanepoel General 7 12-07-2007 09:28 AM
bash "unexpected end of file" script error Runge_Kutta Linux - General 6 05-23-2007 03:36 PM
got a syntax error which shows unexpected end of line when tried to run a shell scrip racer_mec Linux - Newbie 1 01-10-2005 01:43 AM


All times are GMT -5. The time now is 08:19 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