Syntax error near unexpected token 'then'
There must be a space between if
and [
, like this:
#!/bin/bash
#test file exists
FILE="1"
if [ -e "$FILE" ]; then
if [ -f "$FILE" ]; then
echo :"$FILE is a regular file"
fi
...
These (and their combinations) would all be incorrect too:
if [-e "$FILE" ]; then
if [ -e"$FILE" ]; then
if [ -e "$FILE"]; then
These on the other hand are all ok:
if [ -e "$FILE" ];then # no spaces around ;
if [ -e "$FILE" ] ; then # 1 or more spaces are ok
Btw these are equivalent:
if [ -e "$FILE" ]; then
if test -e "$FILE"; then
These are also equivalent:
if [ -e "$FILE" ]; then echo exists; fi
[ -e "$FILE" ] && echo exists
test -e "$FILE" && echo exists
And, the middle part of your script would have been better with an elif
like this:
if [ -f "$FILE" ]; then
echo $FILE is a regular file
elif [ -d "$FILE" ]; then
echo $FILE is a directory
fi
(I also dropped the quotes in the echo
, as in this example they are unnecessary)
syntax error near unexpected token `then'
You should add a space between if
and [
in lines 20 and 25. Your code should be like this.
#!/bin/bash
file=$1
action=$2
if [ -z "$file" ]
then
echo " Please enter a file with your users"
exit 0
fi
if [ -z "$action" ]
then
echo " Please define an account to remove or deleete"
exit 0
fi
for user in `cat $file`
do
if [ "$action" == "add" ]
then
echo adding user: $user
useradd $user -m -p password
fi
if [ "$action" == "remove" ]
then
echo removing user: $user
userdel -r $user
fi
done
Syntax error near unexpected token 'then' in shell script
The script needs a couple minor changes:
#!/bin/sh
yes=y;
no=n;
echo "Do you want to enter batch order id manually? (y/n) "
read answer
if [ "$answer" = $yes ]; then
echo "Please Enter Batch Order Id."
elif [ "$answer" = $no ]; then
echo "Copying all batch orders."
else
echo "please enter correct input."
fi
A space is needed after elif
and before [ $answer -eq $no ]
.
The tests make string comparisons, not numeric comparisons. So, =
is needed in place of -eq
. So that the script works even if the user enters nothing, $answer
is placed inside double-quotes in the tests. Also, a space is required between echo
and "please enter correct input."
.
Why am i getting error eval: syntax error near unexpected token `then' ? BASH
When you run:
STARTUP="if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;"
EVAL_SCRIPT=`eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')`
The $(...)
is evaluated and we end up with:
EVAL_SCRIPT=`eval echo if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;`
Then the backquoted stuff is executed - these three commands:
eval echo if [ -f entrypoint.sh ];
then {{COMMAND}};
fi;
The first command would run find and would echo if [ -f entrypoint.sh ];
but then the second command is invalid because we have a then without a preceeding if. And that's why we get the error.
So if we quote it differently:
STARTUP="if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;"
EVAL_SCRIPT=`eval echo \"$(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')\"`
We get exactly what we wanted assigned to EVAL_SCRIPT: if [ -f entrypoint.sh ]; then ${COMMAND}; fi;
There is only one thing left. You can't execute a variable with just:
# run the script
${EVAL_SCRIPT}
You need to eval a variable. So you need:
# run the script
eval ${EVAL_SCRIPT}
One final note: You don't need all the subshells and evals:
`eval echo "..."`
is completely redundant: eval executes the echo command and that's completely equivalent to just`echo "..."`
`echo "..."`
is completely redundant: the echo is executed and that's completely equivalent to just...
So
STARTUP="if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;"
EVAL_SCRIPT=`eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')`
is equivalent to just:
STARTUP="if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;"
EVAL_SCRIPT=$(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')
And in the end, this works just fine:
#!/bin/bash
STARTUP="if [ -f entrypoint.sh ]; then {{COMMAND}}; fi;"
EVAL_SCRIPT=$(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')
# run the script
eval ${EVAL_SCRIPT}
syntax error near unexpected token `then' in .bash_profile
The output of cat -v
suggest that your file is formatted using only carriage return character as the line endings. This is something unusual. More information about line endings can be found on wiki newline. It would be wise to check the encoding and settings of the editor you used to create and/or edit the file. You may also try inspect the file in a hex editor or for example with hexdump -C
.
Bash parses files expecting only the line feed character to be used as the line ending. Because bash finds none line feed characters, he parses the whole file as a single line.
To replace all carriage return characters to line feed characters, you can use tr
:
tr '\r' '\n' < ~/.bash_profile > tempfile
mv tempfile ~/.bash_profile
Or for example with gnu sed:
sed -i -e 's/\r/\n/g' ~/.bash_profile
It may be possible your file is formatted using dos line endings - ie. both carriage return and line feed characters are used to represent a line ending - but you copied the output of cat -v
to the question improperly. Use dos2unix
utility or something like sed -i -e 's/\r//' ~/.bash_profile
to convert the sequence of carriage return + line feed characters into a single line feed.
How to solve the Bash error syntax error near unexpected token ?
The third echo
command has an unterminated string literal:
echo "Please enter the destination directory in which you wish to copy to$
Perhaps you wanted to write this instead:
echo "Please enter the destination directory in which you wish to copy to:"
The fifth echo
command has the same problem.
Also, this statement is not valid:
else "$filename does not exists try again."
Perhaps you wanted to write this instead:
else
echo "$filename does not exists try again."
Also, there is no fi
corresponding to the first if
statement.
Also, the syntax [ "$op" = "M"]
is invalid. You must have a space before the ]
character, like this: [ "$op" = "M" ]
.
You have the same problem again in [ "$op" = "D"]
.
Related Topics
How to Use Gdb to Debug a Running Process
Why Doesn't "Total" from Ls -L Add Up to Total File Sizes Listed
Are There Standards for Linux Command Line Switches and Arguments
Best Way to Find Os Name and Version in Unix/Linux Platform
Is /Usr/Local/Lib Searched for Shared Libraries
Run Script with Rc.Local: Script Works, But Not at Boot
How to Get the Name of the Current Git Branch into a Variable in a Shell Script
Using 'Date' Command to Get Previous, Current and Next Month
Find a Pattern in Files and Rename Them
Where Are Include Files Stored - Ubuntu Linux, Gcc
How to Check If X Server Is Running
Grep a Large List Against a Large File
How Have Both Local and Remote Variable Inside an Ssh Command
Mongodb Gui Client (Cross-Platform or Linux)