0

(I apologize in advance: I don't know whether my problem concerns rather the code syntax or the file system and would - in the latter case -rather fit into a Linux forum)

I'm trying to set up a litte shell script. But as soon as it comes to multiline statements, I came across a strange behaviour.

It took the example blow from a tutorial page:

number=1
if [ $number = "1" ]; then
    echo "Number equals 1"
else
    echo "Number does not equal 1"
fi

That works fine if I connect via PuTTy to my virtual linux machine (openSUSE 13.1) and copy&paste the code. It does what is expected.

But when I create a file named shell_test.sh (connected via SFTP Net Drive) containing the content below

#!/bin/bash
number=1
if [ $number = "1" ]; then
    echo "Number equals 1"
else
    echo "Number does not equal 1"
fi

and call it from the command line with bash shell_test.sh I get an error:

line 7: syntax error near unexpected token `fi'

The same happens with a for loop. The syntax error is then near the token "do".

cis
  • 1,259
  • 15
  • 48
  • 1
    For number comparisons, do not use `=` but `-eq`. See http://www.faqs.org/docs/abs/HTML/comparison-ops.html for more info. – fedorqui Aug 27 '14 at 13:50
  • try -eq option like,if [ $number -eq 1 ]; then – Nachiket Kate Aug 27 '14 at 13:50
  • 1
    In any case, the script works for me with GNU bash Version 4.3.11 as it is without any error. – martin Aug 27 '14 at 13:52
  • 5
    One question, did you create the file under windows? I suspect a wrong line encoding. Try `dos2unix shell_test.sh` and then `bash shell_test.sh` – martin Aug 27 '14 at 13:54
  • I regret that I have but one upvote to give to @martin. – Etan Reisner Aug 27 '14 at 13:55
  • @martin. Yes, I did create the file under windows and that was obviously the problem. Re-creating the file with vi solved the problem (do2unix is not available on my system). I suspected something like this but didn't find the solution myself. – cis Aug 27 '14 at 14:06
  • 1
    @cis, this will get you every time. Download **notepad++** for windows and you can set the end-of-line completion to **Unix** and eliminate the problem (`notepad++` is probably the best editor for you on the windows side) – David C. Rankin Aug 27 '14 at 14:43
  • possible duplicate of [Bash syntax error: unexpected end of file](http://stackoverflow.com/questions/6366530/bash-syntax-error-unexpected-end-of-file) – tripleee Aug 27 '14 at 16:02

0 Answers0