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 03-04-2010, 06:07 AM   #1
araouf
LQ Newbie
 
Registered: Mar 2010
Posts: 1

Rep: Reputation: 0
Bash Error: syntax error near unexpected token `{


Hi,

I am new to shell scripting, bash specifically.
I am trying to run the following script:
Code:
#!/bin/bash

#send notification emails
sendMails(){
	emails=$1 # get all email ids
}


processLine(){
	line="$@" # get all args
	
	echo -e "****************************"
}

# Shell script utility to read a file line line.
# Once line is read it can be process in processLine() function
# You can call script as follows, to read myfile.txt:
# ./readline myfile.txt
# Following example will read line from standard input device aka keyboard:
# ./readline
# -----------------------------------------------
# Copyright (c) 2005 nixCraft <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
### Main script stars here ###
# Store file name
FILE=""
 
# Make sure we get file name as command line argument
# Else read it from standard input device
if [ "$1" == "" ]; then
   FILE="/dev/stdin"
else
   FILE="$1"
   # make sure file exist and readable
   if [ ! -f $FILE ]; then
  	echo "$FILE : does not exists"
  	exit 1
   elif [ ! -r $FILE ]; then
  	echo "$FILE: can not read"
  	exit 2
   fi
fi
# read $FILE using the file descriptors
 
# Set loop separator to end of line
BAKIFS=$IFS
IFS=$(echo -en "\n\b")
exec 3<&0
exec 0<$FILE
while read line
do
	# use $line variable to process line in processLine() function
	processLine $line
done
exec 0<&3
 
# restore $IFS which was used to determine what the field separators are
BAKIFS=$ORIGIFS
exit 0
When trying to execute I always get this error:

Code:
'iors_response.sh: line 4: syntax error near unexpected token `{
'iors_response.sh: line 4: `sendMails(){
In case you need to know the permissions. Here is it:

Code:
-rwxr-xr-x    1 abdelraouf users        5224 Mar  4 06:46 biors_response.sh
I run the script using the following command:
Code:
bash biors_response.sh hosts.txt
That's all. If you need any aditional information, please ask.

Thanks in advance.
 
Old 03-04-2010, 04:43 PM   #2
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
A non-printing character at the end of this line
sendMails(){
could cause such error messages.
For example, if the script has been edited in Windows it could have carriage return characters at the line ends. Carriage returns are written as \r or ^M.
This demonstration uses 'echo -e' and '\r' to create a script with a carriage return character after
sendMails(){
Running the script gives the same error messages.
Code:
echo -e '#!/bin/bash
sendMails(){\r
 echo "Hello"
}
sendMails' > test.sh     # Create the script

chmod a+x test.sh        # Make the script executable
Code:
cat test.sh              # Inspect the script
#!/bin/bash
sendMails(){             # The \r is not visible
 echo "Hello"
}
sendMails

./test.sh                # Run the script
'/test.sh: line 2: syntax error near unexpected token `{
'/test.sh: line 2: `sendMails(){
Code:
cat --show-nonprinting test.sh
#!/bin/bash
sendMails(){^M           # ^M is the same as \r
 echo "Hello"
}
sendMails
 
  


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: syntax error near unexpected token ty1on Linux - Newbie 3 08-26-2009 02:19 PM
sh: syntax error near unexpected token `(' venkatesh_b Linux - Newbie 1 05-16-2009 05:44 AM
syntax error near unexpected token -what does this mean? rmnature Linux - Newbie 5 02-14-2009 08:19 AM
syntax error near unexpected token in bash using perl kambrish Programming 21 07-03-2007 12:42 PM
Trouble with Bash -- syntax error near unexpected token `fi' anamericanjoe Programming 5 05-19-2006 02:59 PM

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

All times are GMT -5. The time now is 05:31 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