LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 03-21-2012, 07:46 PM   #1
pasd
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Rep: Reputation: 0
Bash Script syntax error: unexpected end of file


hello everyone,

this question has been asked on this forum a 100 times. I went through all of the posts but here is a simple piece of code and am unable to figure my error. Can someone pls help?


#!/bin/sh --debug
build_dir=`dirname $0`
export BUILD_ID=SCAQBAF
export BUILD_VER=0111
if [ "$BUILD_ID" = "SCAQBAF" ]
then
echo hai
fi
echo bye
exit 0


i dont see any error if i remove everything after the "if" stmt. What is wrong with the if syntax here?
 
Old 03-21-2012, 07:57 PM   #2
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

that works for me if I remove the "--debug". This prompts the question what is /bin/sh linked to on your system? Eg
Code:
readlink -f /bin/sh
Evo2.
 
1 members found this post helpful.
Old 03-21-2012, 08:00 PM   #3
pasd
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: 0
thanks evo2 for replying!

Its bash shell


ls -ltr /bin/sh
lrwxrwxrwx 1 root root 4 Feb 25 2010 /bin/sh -> bash
 
Old 03-21-2012, 08:07 PM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

ok, that script should work with bash. So, next guess is that the script you are running is somehow subtly different to what you pasted into your post. Could you perhaps attach the actual file and post it here? (The "Manage Attachments" button in the full or "advanced" posting interface, or if you do "Preview Post")

Evo2.
 
Old 03-21-2012, 08:11 PM   #5
pasd
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: 0
Have attached the file as txt
Attached Files
File Type: txt dummy.txt (155 Bytes, 85 views)
 
Old 03-21-2012, 08:12 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Well, I don't get any errors for sh or bash, with or without --debug.
Possibly a version thing.??
My /bin/sh links to /bin/bash which is
Code:
bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)
As a personal thing, I always use [[ ]] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

PS you can check the actual content thus
Code:
od -c t.sh

Last edited by chrism01; 03-21-2012 at 08:13 PM.
 
1 members found this post helpful.
Old 03-21-2012, 08:40 PM   #7
pasd
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: 0
Bash version I have is

GNU bash, version 3.2.48(1)-release (x86_64-pc-linux-gnu)

PS adding [[ gives me syntax error on this version.
 
Old 03-21-2012, 08:56 PM   #8
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
The file you attached uses DOS line endings. Emacs reported that when I opened it.

In brief, a file written and saved in DOS/Windows uses a special character sequence to indicate the end of a line. Linux uses Unix line endings: a subset of the DOS/Windows character sequence. The consequence of this is, when Linux tries to read a DOS/Windows file, Linux "sees" more characters than you would expect. The extra character is not printable--you cannot see it when you examine the file in a text editor. It is this extra character per line that causes your problem.

I was able to reproduce your "unexpected end of file" error by running
Code:
user@localhost$ bash dummy.txt
dummy.txt: line 11: syntax error: unexpected end of file
using your unmodified, attached file.

You need to convert the line endings in the file to Unix-style line endings with the dos2unix command or with this simple sed command:
Code:
sed -i 's@\r@@' dummy.txt
If you edit the file with a DOS/Windows editor, you will probably need to convert the line endings every time you save your edits.

I'd suggest you get acquainted with native Linux text editors and work with them for Linux scripts/configuration files.

Last edited by Dark_Helmet; 03-21-2012 at 09:02 PM.
 
1 members found this post helpful.
Old 03-21-2012, 08:57 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Definitely try the od cmd; even in bash 3 I'm pretty sure [[ ]] should work.


... and DarkHelmet said what I was thinking: DOS line-endings; a common mistake.

Last edited by chrism01; 03-21-2012 at 08:59 PM.
 
Old 03-21-2012, 10:19 PM   #10
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

indeed was was dos line endinings:

Code:
% readlink -f /bin/sh
/bin/dash
% ./downloads/dummy.txt
/bin/sh: 0: Can't open 
% /bin/bash ./downloads/dummy.txt
./downloads/dummy.txt: line 11: syntax error: unexpected end of file
% dos2unix ./downloads/dummy.txt
dos2unix: converting file ./downloads/dummy.txt to Unix format ...
% ./downloads/dummy.txt
hai
bye
% bash ./downloads/dummy.txt
hai
bye
Cheers,

Evo2.
 
Old 03-22-2012, 10:38 AM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Since the main problem has been diagnosed, I'd like to post a few comments about the script itself in the OP.

1) First of all, please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


2) Clean, consistent formatting makes code readable and more easily debuggable. Indent all your sub-commands, and separate logical sections with whitespace. Add comments anywhere the code isn't completely obvious (and remember, what seems obvious to you now will not be a year or so down the line).

Many people also think that it makes it more readable to place the "do/then" keywords on the same line as the "for/while/until/if" keywords. It more clearly separates the outside block from the inside block.

Code:
for var in <list> ; do
	<commands>
done

3) #!/bin/sh is used for interpreting scripts in posix-compliant lowest-common-denominator mode. This is mostly recommended for system startup scripts and other cases where portability and standardization are more important than convenience. When this isn't required, you should probably use #!/bin/bash or another shell that has more modern, advanced features available, such as ksh or zsh.


4) $(..) is highly recommended over `..`


5) Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.
 
1 members found this post helpful.
Old 03-26-2012, 07:21 PM   #12
pasd
LQ Newbie
 
Registered: Mar 2012
Posts: 5

Original Poster
Rep: Reputation: 0
thank you all for helping me get resolved with this trivial error! Much appreciated will keep in mind these comments.
 
  


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
bash script syntax error: unexpected end of file? smecnb Linux - Newbie 11 02-25-2013 12:44 AM
-bash: *.sh: line 25: syntax error: unexpected end of file prashanth212 Linux - General 8 04-05-2010 11:52 PM
bash line 74: syntax error: unexpected end of file help? andycol Linux - General 5 09-14-2009 08:12 AM
Bash script -----------syntax error: unexpected end of file ArthurHuang Programming 2 05-01-2009 10:29 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 06:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 03:13 PM.

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