[SOLVED] Shell Scripting "syntax error : unexpected end of file" in case of a if/else condition
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm sorry, that script works for me both with sh and bash. Would be nice to give a better description of that problem. How can you reproduce it exactly?
In debian the #!/bin/sh runs the script in "dash", not bash. Use #!/bin/bash if you need the "bash" extras. In spite of what practices and books have said about bash scripting for the past twenty-ish.
"syntax error : unexpected end of file" usually means that there isn't an End Of Line (EOL) character on the last line of your script. Editing the file and moving the cursor to the end of the last line and pressing the enter key should fix the error.
"syntax error : unexpected end of file" usually means that there isn't an End Of Line (EOL) character on the last line of your script. Editing the file and moving the cursor to the end of the last line and pressing the enter key should fix the error.
Also, speaking to the more-general case of "programming languages, not necessarily Bash," all programming tools start by passing your source-code through a so-called parser, which basically tries to match-up the source that you have written against the known structure of the language (expressed by a so-called grammar). But you can make certain syntax errors – such as, say, an un-closed quote for a string literal near the end of the program – which will cause the parser to "stupidly run away" and crash headlong into the end of the file. It can be rather-annoyingly tricky to pinpoint the actual location of the problem.
"syntax error : unexpected end of file" usually means that there isn't an End Of Line (EOL) character on the last line of your script. Editing the file and moving the cursor to the end of the last line and pressing the enter key should fix the error.
Still facing the same issue,actually trying in Linux VM using Putty.
I have tried all possible ways to solve it,
./demo.sh: /bin/bash^M: bad interpreter: No such file or directory
facing this.
But on removing this "!/bin/bash"...it seems to run the program.But still the issue "Shell Scripting "syntax error : unexpected end of file"" seems to persist.
Welcome to LQ!!! The ^M is the 'clue'!!! Here's something to web-research: dos2unix
Quote:
I found the problem - carriage-return characters in the file.
I was typing the script in Windows (text pad) and executing it on Linux. The editor on Windows ends lines with \r\n; when running the script on Linux, the presence of \r was creating a problem.
Using vi as editor helped me resolve this issue.
Code:
$ echo -e '#!/bin/sh\r' > bad.sh
$ chmod 755 bad.sh
$ sh bad.sh
$ ./bad.sh
bash: ./bad.sh: /bin/sh^M: bad interpreter: No such file or directory
$ cat bad.sh
#!/bin/sh
$ od -ct x1 bad.sh
0000000 # ! / b i n / s h \r \n
23 21 2f 62 69 6e 2f 73 68 0d 0a
0000013
$
./demo.sh: /bin/bash^M: bad interpreter: No such file or directory - this is solved in #10
"syntax error : unexpected end of file"
Are two different, unrelated issues. Would be nice to post your script - exactly what you tried to execute - and also would be nice to explain exactly how can we reproduce that error.
Try: od -c demo.sh | nc termbin.com 9999 #and post link
and look for extra \r
Quote:
explain exactly how can we reproduce that error.
Put a \r==^M at end of last line (fi), instead \n, and you WILL get that error!!!
Code:
$ od -ct x1 demo.sh |tail -4
20 64 69 72 65 63 74 6f 72 79 20 6e 6f 74 20 66
0000140 o u n d ! " \n f i \r
6f 75 6e 64 21 22 0a 66 69 0d
$ ./demo.sh
./demo.sh: line 6: syntax error: unexpected end of file (expecting "fi")
Try: od -c demo.sh | nc termbin.com 9999 #and post link
and look for extra \r
Code:
[root@ip-xxx-xx-xxx-xx tmp]# od -c demo.sh
0000000 i f " $ ( p s - e f | g r e
0000020 p c o m m v a u l t ) " >
0000040 / d e v / n u l l \r \n t h e n \r
0000060 \n e c h o " R u n n i
0000100 n g " \r \n e l s e \r \n e
0000120 c h o " S t o p p e d " \r \n f
0000140 i \r \n
0000143
Quote:
Sorry but can you please help me understand.
I am getting security alerts while trying to post.
./demo.sh: /bin/bash^M: bad interpreter: No such file or directory - this is solved in #10
"syntax error : unexpected end of file"
Are two different, unrelated issues. Would be nice to post your script - exactly what you tried to execute - and also would be nice to explain exactly how can we reproduce that error.
Code:
if [ -d "$/abc/de" ];
then
echo "$/abc/de directory exists!"
else
echo "$/abc/de directory not found!"
fi
I have already posted this,this is a simple if/else statement that does not work in a Linux VM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.