LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 11-06-2017, 04:20 AM   #1
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Rep: Reputation: Disabled
Question Shell Scripting "syntax error : unexpected end of file" in case of a if/else condition


if [ -d "$/abc/de" ];
then
echo "$/abc/de directory exists!"
else
echo "$/abc/de directory not found!"
fi

##note i have not used "#!/bin/sh"
will that cause the issue.
Using #!/bin/sh is causing different issue
 
Old 11-06-2017, 04:39 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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?
 
Old 11-06-2017, 04:59 AM   #3
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874Reputation: 874
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.
 
Old 11-06-2017, 05:24 AM   #4
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
"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.
 
Old 11-06-2017, 07:52 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940Reputation: 3940
Quote:
Originally Posted by michaelk View Post
"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.
 
Old 11-06-2017, 08:00 AM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,700

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
True, but I did post usually...
 
Old 11-07-2017, 10:36 PM   #7
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
"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.
 
Old 11-07-2017, 10:40 PM   #8
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-08-2017, 12:23 AM   #9
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
please post output of
Code:
echo $SHELL
if that's empty, try
Code:
ls -al /bin/sh
and any other relevant information.

Last edited by ondoho; 11-08-2017 at 12:24 AM.
 
Old 11-08-2017, 12:40 AM   #10
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
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
$

Last edited by !!!; 11-08-2017 at 04:05 AM.
 
Old 11-08-2017, 01:02 AM   #11
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ondoho View Post
please post output of
Code:
echo $SHELL
if that's empty, try
Code:
ls -al /bin/sh
and any other relevant information.
Output of
Code:
echo $SHELL
Code:
[root@ip-xxx-xx-xxx-xx ~]# echo $SHELL
/bin/bash
 
1 members found this post helpful.
Old 11-08-2017, 01:45 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
./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.
 
Old 11-08-2017, 02:18 AM   #13
!!!
Member
 
Registered: Jan 2017
Location: Fremont, CA, USA
Distribution: Trying any&ALL on old/minimal
Posts: 997

Rep: Reputation: 382Reputation: 382Reputation: 382Reputation: 382
A web-search of the error: "syntax error : unexpected end of file"
finds this may be due to:
Quote:
the shell can also emit this message when the final line of your script lacks a newline.
https://stackoverflow.com/questions/...-file/17145123

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

Last edited by !!!; 11-08-2017 at 03:11 AM.
 
Old 11-08-2017, 03:04 AM   #14
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Original Poster
Rep: Reputation: Disabled
Unhappy

Quote:
Originally Posted by !!! View Post
A web-search of the error: "syntax error : unexpected end of file"
finds this may be due to:
https://stackoverflow.com/questions/...-file/17145123

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.
 
1 members found this post helpful.
Old 11-08-2017, 03:10 AM   #15
Anna$9
LQ Newbie
 
Registered: Nov 2017
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View 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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Shell script to cause wifi scan get "unexpected end of file" message Ed1952 Linux - Software 5 11-27-2015 09:39 AM
[SOLVED] why wont my code work, " syntaX error: unexpected end of file" jsf1337 Linux - Newbie 5 03-01-2015 11:23 AM
[SOLVED] Script returning with error "syntax error: unexpected end of file" n_raghuvanshi Linux - Software 4 08-25-2013 08:49 AM
[SOLVED] Shell Scripting "syntax error : unexpected end of file" roxie600 Programming 12 04-30-2010 12:18 AM
Backup Script error "line 31: syntax error: unexpected end of file" eswanepoel General 7 12-07-2007 09:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:26 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration