LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Scripting "syntax error : unexpected end of file" (http://www.linuxquestions.org/questions/programming-9/shell-scripting-syntax-error-unexpected-end-of-file-804990/)

roxie600 04-29-2010 10:24 AM

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 :)

deadeyes 04-29-2010 10:35 AM

Quote:

Originally Posted by roxie600 (Post 3951970)
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, ...

catkin 04-29-2010 11:03 AM

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.

roxie600 04-29-2010 11:20 AM

hey
i copied and pasted your code and i still got the same error - i also tried using [[ but still got the same error

ilikejam 04-29-2010 11:44 AM

Works for me.

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

Dave

catkin 04-29-2010 12:30 PM

The #usr/bin/sh is strange. Try changing it to #!/bin/bash

paulsm4 04-29-2010 12:43 PM

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

roxie600 04-29-2010 02:19 PM

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 :)

paulsm4 04-29-2010 02:42 PM

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.

roxie600 04-29-2010 02:58 PM

Am still getting the end of file unexpected error (it saved in the wrong place before so was still running the old one)

paulsm4 04-29-2010 04:49 PM

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

roxie600 04-29-2010 05:30 PM

Thank you so much! That's now working fine. I will continue working that way from now on.
Thanks again for your help :)

grail 04-30-2010 12:18 AM

Please mark as SOLVED


All times are GMT -5. The time now is 07:39 AM.