Reputation: 81
This shell script is throwing an error message :
syntax error near unexpected token do
Here's my code
#!/bin/bash
DIRS="/home/delhi_qa/mkv/18000/marketview/log/FIFO_OPTIMISER/LOGS 1"
DEST="home/delhi_qa/Tests/Ankit/TTLOGSBACKUP"
DELETE_OLD_ZIP_FILES="no"
BASENAME=/usr/bin/basename
ZIP=/usr/bin/zip
for i in $DIRS
do
#On running this unexpected token message occurred at this line#
zipfile="${DEST}/$(${BASENAME} ${d}).zip"
echo -n "Creating $zipfile..."
if [ "$DELETE_OLD_ZIP_FILES" == "yes" ]
then
[ -f $zipfile ] && /bin/rm -f $zipfile
fi
${ZIP} -r $zipfile $d &>/dev/null && echo "Done!"
done
Can you tell me why?
Upvotes: 0
Views: 8871
Reputation: 104
Easy way to convert example.sh file to unix is use NotePad++ (Edit>EOL Conversion>UNIX/OSX Format)
You can also set the default EOL in notepad++ (Settings>Preferences>New Document/Default Directory>select Unix/OSX under the Format box)
Reference- syntax error near unexpected token `$'in\r''
It also solves this problem: android_build.sh gives error while building ffmpeg library
I used this one. It solves my problem earlier.
devnull and Armali already pointed to the problem. Hope the details will help you.
Upvotes: 1
Reputation: 19395
Your input file contains carriage returns. Use dos2unix or something similar to get rid of those. – devnull
devnull is spot-on. The bad revision 3 of the question hides this by distorting the error message, which previously read
syntax error near unexpected token `do- note the lone grave accent (`), which
bash
uses at the beginning of token names in messages together with an apostrophe (') at the end to quote the token.
The exact error message got to be e. g. for a script called ./do
'/do: line 8: syntax error near unexpected token `do
The appendant apostrophe appears at the start of the line, having overwritten the first character of the command ./do
. This can only be caused by a carriage return following do
.
Upvotes: 1
Reputation: 523
Use: DIRS="/home/delhi_qa/mkv/18000/marketview/log/FIFO_OPTIMISER/LOGS\ 1"
Check additional '\' after LOGS as linux will not take space in consideration if '\' is not used. May be thats causing the error. To confirm its proper working you can also, check with different folder name to be present, e.g LOG_1.
Upvotes: 0