0

I have a small shell script which is throwing error:

unexpected end of file

The script follows:

#!
if [  t -eq  1  ]
then
 echo " TEST1 "
else
echo " TEST LAST LOOP "
fi
  • What's the output of sed -n l < your-script? – Stéphane Chazelas Mar 23 '18 at 17:11
5

There is nothing in the script that should give that error. If you run the script with sh ./script.sh, it will produce

 TEST LAST LOOP 

as output. If running it with bash ./script.sh you will get the same output, but with an additional "integer expression expected" error since t is not an integer (this may also happen if you run with sh if your sh is implemented by bash). If you run it with ./script.sh, the shell will most likely complain with "No such file or directory" since you have not added a proper #!-line.

In this script, you may use #!/bin/sh as the #!-line as you only use standard POSIX features.

The test [ t -eq 1 ] will never be true as t (the character) is not an integer. If you're setting the variable t to an integer somewhere, use [ "$t" -eq 1 ] in the test.


The only way I can get a shell to say "unexpected end of file" is by converting the script to a DOS text file and running it with bash.

If you get this error, then the script file is probably a DOS text file (possibly due to being created on a Windows machine). You should run dos2unix on the script.

See questions related to dos2unix.

  • 3
    [ t -eq 1] may be true with some sh implementations if a t environment variable has been defined with a value that is an arithmetic expression that resolves to 1. For instance, t=20/20 ksh93 -c '[ t -eq 1 ]' is true. t='a[$(")]' ksh93 -c '[ t -eq 1 ]' would give you a 'end of file' unexpected error. – Stéphane Chazelas Mar 23 '18 at 17:06
  • 1
    Having said that, I agree the DOS text file format is a much more likely explanation – Stéphane Chazelas Mar 23 '18 at 17:11
  • @StéphaneChazelas Nicely done with that ksh93 thing, but the wording of the error message won't be exactly the same though. – Kusalananda Mar 23 '18 at 17:15
0

Missing the shebang line #!/bin/sh and t isn't defined

  • 1
    None of those things would provoke an "unexpected end of file" error. – Kusalananda Mar 23 '18 at 7:52
0
  • replace #! by #!/bin/sh or run sh ./script.sh, and you must set t as integer $t not t.
  • The answer is spot on, but it could use with a bit of expansion on the reasons for it – Bruno9779 Mar 23 '18 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.