Bash Syntax Error: Unaccepted Token Elif

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,
the consequent-commands list is executed. If test-commands returns a
non-zero status, each elif list is executed in turn, and if its exit
status is zero, the corresponding more-consequents is executed and the
command completes. If ‘else alternate-consequents’ is present, and the
final command in the final if or elif clause has a non-zero exit
status, then alternate-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 :
shellcheck

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



Leave a reply



Submit