Syntax error near unexpected token `elif'
It's your line endings. Transferring it from Windows has left the CR/LF
line endings on.
When I create a script then manually add the CR
characters, I get exactly the same error:
qq.sh: line 3: syntax error near unexpected token `elif'
'q.sh: line 3: `elif [ 1 == 1 ] ; then
You can fix it by removing the CR character from CR/LF line endings.
cat script.sh | sed 's/\r$//' >newscript.sh
syntax error near unexpected token `elif' error in bash
You need a semi-colon after if [ ... ]
and before then
, and the same with elif
:
if [ "$1" == "this_script" ]; then
# ^
# here!
# v
elif [ "$1" == "other_script" ]; then
From Bash manual - 3.2.4.2 Conditional Constructs:
The syntax of the if command is:
if test-commands; then
consequent-commands;
[elif more-test-commands; then
more-consequents;]
[else alternate-consequents;]
fi
The
test-commands
list is executed, and if its return status is zero,
theconsequent-commands
list is executed. Iftest-commands
returns a
non-zero status, eachelif
list is executed in turn, and if its exit
status is zero, the correspondingmore-consequents
is executed and the
command completes. If ‘else alternate-consequents
’ is present, and the
final command in the finalif
orelif
clause has a non-zero exit
status, thenalternate-consequents
is executed. The return status is
the exit status of the last command executed, or zero if no condition
tested true.
Bash : Syntax error near unexpected token `elif' with if/then blocks w/ only comments
No, bash thinks this is invalid. Turns out you cant have empty clauses as the error was really pointing to the first if statement because I didn't actually put anything inside it, just a placeholder.
I then checked ShellCheck.net to see what's really going on and here it is :
To fix this, simply just put any block of code as the statements as the placeholder comments are invalid.
#!/bin/bash
if [[ $1 = "add" || $1 = "-a" ]]; then
echo add
elif [[ $1 = "done" || $1 = "-d" ]]; then
echo done
elif [[ $1 = "show" || $1 = "-s" ]]; then
echo show
elif [[ $1 = "clear" || $1 = "-cl" ]]; then
echo clear
elif [[ $1 = "help" || $1 = "-h" ]]; then
showHelp
else
showHelp
fi
Syntax error near unexpected token 'elif' in Bash
This will do what you want:
#!/bin/bash
function add {
echo $(( $1 + $2 ))
}
if [ $3 = '+' ]; then add $1 $2
fi
Syntax error near unexpected token elif in bash
You need to close both of your inner if-else statements with a fi
.
For example:
if [ $output2 = "Show"]
then
echo showing files
else
echo hiding files
fi
You also need a space before the closing ]
in your if conditions. For example:
if [ $output2 = "Show" ]
How can I fix shell error syntax error near unexpected token 'elif'
First of all, do not tag it bash
and sh
, you have one shell, type echo $SHELL
to know which shell you use, or put a shebang at the start of your script (#!/usr/bin/env bash
)
put semicolons after your commands, including [
... ]
which is an alias for test
. Command terminators are newline, ;
, &&
, ||
and &
and are mandatory. You can put several commands between if
and then
, so those semicolons are mandatory.
if [ -f "$PROJECT_DIR/build/Release-macosx/UnicodeEscape" ] ; then
build/Release-macosx/UnicodeEscape "en.lproj/Localizable.strings" ;
elif [ -f "$PROJECT_DIR/build/Debug-macosx/UnicodeEscape" ] ; then
build/Debug-macosx/UnicodeEscape "en.lproj/Localizable.strings" ;
fi
syntax error near unexpected token `elif' (` elif [ $ver == 8]; then')
elif [ $ver == 7]; then:
version="1.8.8"
elif [ $ver == 8]; then
version="1.8.7"`
in the line elif [$ver == 7]; then:
you have :
after then which is caused the error. just delete the :
and put space before each ]
in conditions.
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 Run Multiple Programs in a Sequence
Bash -C Variable Does Not Get Assigned
Print Differences of File1 to File2 Without Deleting Anything from File2
Phusion Passenger Nginx Module Installer V3.0.17 Issue on Debian 6.0.5 Amd64 Due to Broken Package
Update Specific Field in Text File in Specific Line
How to Route Webcam Video to Virtual Video Device on Linux (Via Opencv)
How to Add Text After Last Pattern Match Using Ed
Allocating a Data Page in Linux with Nx Bit Turned Off
Script to Get User That Has Process with Most Memory Usage
How to Create a Bash Variable Like $Random
How to Merge Two Rows in a Same Row from a Text File in Linux Shell Script
A Way to Prevent Bash from Parsing Command Line W/Out Using Escape Symbols
Adding Timestamps to Packet Payload with Tcpreplay
Find Files with a Certain Extension That Exceeds a Certain File Size
How to Automate Measuring of Bandwidth Usage Between Two Hosts