1

Source code:

#!/bin/bash
echo "Parametro is $1"
if [ $1 -gt 9 ]
    then
    echo "entre al if";
fi
echo"Fin del script";

My bash file has execute permission:

 -rwxrwxr-x 1 mzadmin mzadmin 108 Feb 15 13:07 test.sh

I run my bash file like:

bash test.sh 9

And the output is:

Parametro is 9
test.sh: line 8: syntax error: unexpected end of file.
3
  • The posted script doesn't have a line 8. Commented Feb 15, 2018 at 20:00
  • @chepner Interestingly, the error does get reported on line 8. This happens both with and without a terminating linefeed on line 7. Commented Feb 15, 2018 at 20:43
  • 1
    This is probably due to carriage returns. See Why is a shell script giving syntax errors when the same code works elsewhere? Commented Feb 15, 2018 at 21:00

2 Answers 2

2

You need a space after echo command : echo"Fin del script";

So :

echo "Fin del script"

Next time, before asking human help, please pass your script online on http://www.shellcheck.net/ (even if this particular error is not detected, because the shell think echo"Fin del script"; is a unknown command...)

Edit

If you don't have output it's because you use in your test -gt (is greater) but instead you need -ge is greater or equal.

Better use bash arithmetic like this, it's more readable and less error prone :

if ((arg >= 9)); then
Sign up to request clarification or add additional context in comments.

2 Comments

thaks for your answer, I checked my code in shellcheck.net and I found other mistake: Line 3: if [ $1 -gt 9 ] ^-- SC2086: Double quote to prevent globbing and word splitting. I solved this new mistake and the output is the same. ;(
Check my edited post
0

You need to put a newline at the end of your script, because sometimes things don't work properly if they can't find a newline character. See Why should text files end with a newline?

2 Comments

thaks, but I solved this mistake and the output is the same ;( ;(. #!/bin/bash echo "Parametro is $1" if [ "$1" -gt 9 ] then echo "entre al if" fi echo "Fin del script" I put two newline at the end. :(
thanks guys. I solved my error. the source code is ok, but I am working in windows and after put my file.sh to the Server linux and something is wrong with this case. If I create my file.sh in the server using "vi filename.sh" and I type the same source code, save and after execute "bash filename.sh" everything works fine.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.