I am facing the syntax error near unexpected token,elif error for the following code.
if [ "$1" == "abc" ]; then
echo "abc"
elif [ "$1" == "xyz" ]; then
echo "xyz"
else
echo "Unkown parameter"
exit 0
fi
Error is:
abc.sh: line 28: syntax error near unexpected token elif'
abc.sh: line 28:elif [ "$1" == "xyz" ]; then
Code seems alright but only point I would like to add is the OS on which you are running. The == works fine with Linux RedHat/Suse or some solaris machine but on some OS like Hpux or AIX it don't work. You should use = which is correct as you are comparing strings.
Related
I've implemented script like below:
podName=someValue ; // Here value is dynamic (empty or non empty)
if[ $podName == " "] then
echo "Empty"
Even though I got empty an output but still could see:
if[ == ] : not found [No such file or directory ]
error message while running the script.
Seems like there is a small formatting issue which is causing this error. This should be the correct code.
podName=someValue
if [ "$podName" == " " ]; then
echo "Empty"
fi
You can check if the string is empty in bash / sh like this (some containers don't have bash shell):
podName="someValue"
if [ -z "$podName" ]; then
echo "Empty"
fi
or:
podName="someValue"
if [ "$podName" = "" ]; then
echo "Empty"
fi
The following syntax works only in bash [ "$podName" == "" ]
Error: unexpected EOF while looking for matching `"'
Code:
#!/bin/bash
qr_heisys=$1
testpath='/home/home/Heisys/tester/app/dut/variant'
if [[ $testdesc == "-h" ]]
then
echo "script to write data to Board EEPROM of Heisys"
echo "eeprom is at address 0x55"
echo "first argument is testdesc"
echo "second argument is qr"
echo "usage: write_eeprom.sh "write to eeprom" ""$qr_heisys"""
exit 0
fi
# teststep "${testdesc}" TSN: not needed
echo "qr_heisys = ""$qr_heisys"
echo "${testpath}${qr2eeprom}qr2eeprom.py "$qr_heisys"""
if [[? -ne "0"]]
then
return $EXIT_FAIL "Error writing QR Code to eeprom"
else
return $EXIT_PASS "Write QR to Boardeeprom was successfully PASS"
fi
I try to execute the code using ./write_eeprom.sh command on the terminal.
When googled the issue, i was mentioned about parsing error or double quote. I tried , but I am still not getting what is the issue.
Please can someone advice.
I used shellcheck.net too
Normally, in bash, if I wanted to loop until a command produced a certain output I'd do this:
while : ; do
status=$(mycommand)
[ "$status" = "whatever" ] && break
printf "."
sleep 10
done
But how would I do that I wanted to loop until I got stderr output? I thought maybe I could do this:
while : ; do
status=$(mycommand 2>&1)
[ "$status" = "whatever" ] && break
printf "."
sleep 10
done
But when I do that I get this error:
-bash: syntax error near unexpected token `2' error.
Any ideas?
My first question is why putting ! in the front of if statement fails to produce syntax error when status is not double quoted. That is how is [ ! $x = "string" ] different from
[ $x != "string" ]?
My script is as follows.
#!/bin/bash
status=" "
# Comparison 1: without error
echo "-----Comparison 1-----"
if [ ! $status = "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 2-----"
# Comparison 2: with error message but still shows success
if [ $status != "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 3-----"
# Comparison 3: Correct result after quoting status
if [ ! "$status" == "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
echo "-----Comparison 4-----"
# Comparison 4: Correct result after quoting status
if [ "$status" != "success" ]
then echo 'Error: status was not success but: ' $status
else
echo "The status is success."
fi
The output is
-----Comparison 1-----
The status is success.
-----Comparison 2-----
./test2.sh: line 14: [: !=: unary operator expected
The status is success.
-----Comparison 3-----
Error: status was not success but:
-----Comparison 4-----
Error: status was not success but:
Additional questions
Why does it produce "The status is success." in Comparison 2 after a syntax error? How would a syntax error in an if statement affects the evaluated result of such if statement?
p.s. I know we need "" around $status to make output right.
You need to quote $status. Without the quotes, your first comparison is
if [ ! = "success" ]
which will fail because ! and success are not equal strings.
Your second one results in a syntactically invalid expression for [:
if [ != "success" ]
The syntax error you see in condition 2 isn't a shell syntax error, but a condition syntax error raised by the [ command. The command itself runs fine, but exits with a non-zero exit status.
I am trying to write a shell script that will either start or stop openvpn, depending on what the user enters. When I run the script it correctly prompts me for what I want to do. But when I type "1" and hit [Enter] it outputs this and terminates:
./control-openvpn.sh: 27: ./control-openvpn.sh: Syntax error: "fi" unexpected
Here is the main part of my code. The functions are above it in the script.
# Main
echo -n "What to do? 1.Start or 2.Stop. [1/2]"
read action
if [ "$action" == "1" ]
then
start_openvpn()
elif [ "$action" == "2" ]
then
stop_openvpn()
fi
Thank you in advance
In bash, when you do start_openvpn(), you are declaring a new function. Thus, bash gets confused when the next thing it sees is fi. Something like this should work for you:
read -p 'What to do? 1.Start or 2.Stop. [1/2] ' action
if [ $action -eq 1 ]; then
start_openvpn
elif [ $action -eq 2 ]; then
stop_openvpn
fi