I'm trying to create simple script to check whether my external ip address has changed. However, I keep getting the following error:
syntax error near unexpected token `else'
This is my code:
#!/bin/bash
DESTDIR=exip.txt
PREVIP=$(<$DESTDIR)
EXIP=$(dig +short myip.opendns.com #resolver1.opendns.com)
echo "External ip checker schript V1.0"
echo
echo
echo "Previous ip: $PREVIP"
echo
echo "Your current external ip is $EXIP"
if [ "$PREVIP" == "$EXIP"]:then
echo "Both IP-adresses are the same"
else
echo "The IP addresses are diffrent"
echo "Sending autoremote message.."
curl "http://autoremotejoaomgcd.appspot.com/sendmessage?key=APA91bEAg6VebS03KS$
echo "$EXIP" > "$DESTDIR"
fi
I've looked ad other topics about this error but i just can't figure it out.
The error is here:
if [ "$PREVIP" == "$EXIP"]:then
^^
you need to add spaces around [ and ] and finally use semicolon:
if [ "$PREVIP" == "$EXIP" ]; then
^^^
More generally, you can always use a tool called Shellcheck to check if your script has these common errors. It is both a command and a website, so you can paste your script in http://www.shellcheck.net/.
Related
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
I want to run this command source .env (sourcing a .env file) and if the .env file had some errors while sourcing. I want to show a message before the error output "Hey you got errors in your .env" else if there's no error, I don't want to show anything.
Here's a code sample that needs editing:
#!/bin/zsh
env_auto_sourcing() {
if [[ -f .env ]]; then
OUTPUT="$(source .env &> /dev/null)"
echo "${OUTPUT}"
if [ -n "$OUTPUT" ]; then
echo "Hey you got errors in your .env"
echo "$OUTPUT"
fi
}
You could use bash -n (zsh has has a -n option as well) to syntax check your script before sourcing it:
env_auto_sourcing() {
if [[ -f .env ]]; then
if errs=$(bash -n .env 2>&1);
then source .env;
else
printf '%s\n' "Hey you got errors" "$errs";
fi
fi
}
Storing the syntax check errors in a file is a little cleaner than the subshell approach you have used in your code.
bash -n has a few pitfalls as seen here:
How do I check syntax in bash without running the script?
Why not just use the exit code from the command source ?
You don't have to use bash -n for this because ...
If let's say your .env file contains these 2 invalid lines:
dsadsd
sdss
If you run your current accepted code using the example above:
if errs=$(bash -n .env 2>&1);
the above condition will fail to stop the file from sourcing.
So, you can use source command return code to handle all of this:
#!/bin/bash
# This doesn't actually source it. It just test if source is working
errs=$(source ".env" 2>&1 >/dev/null)
# get the return code
retval=$?
#echo "${retval}"
if [ ${retval} = 0 ]; then
# Do another check for any syntax error
if [ -n "${errs}" ]; then
echo "The source file returns 0 but you got syntax errors: "
echo "Error details:"
printf "%s\n" "${errs}"
exit 1
else
# Success here. we know that this command works without error so we source it
echo "The source file returns 0 and no syntax errors: "
source ".env"
fi
else
echo "The source command returns an error code ${retval}: "
echo "Error details:"
printf "%s\n" "${errs}"
exit 1
fi
The best thing with this approach is, it will check both bash syntax and source syntax as well:
Now you can test this data in your env file:
-
~
#
~<
>
I am trying to write a script to toggle HAproxy to reroute a user to a maintenance page if a service is down. Here is the part I am having trouble with:
if [ "$idIsValid" = "false" ]; then
if [ "$(cat isUp)" = "true" ]; then
echo "Site is down! Routing to maintenance page."
echo false > isUp
mv haproxy.cfg haproxy.cfg.temp
mv haproxy.cfg.other haproxy.cfg
mv haproxy.cfg.temp haproxy.cfg.other
#restart haproxy
service haproxy restart
echo "Restarting HAproxy"
elif [ "$(cat isUp)" = "false" ]; then
#do nothing since it has already changed
echo "Nothing to do; service is still down."
else
#notify me that isUp is set to something other than true or false, or something else is wrong.
fi
fi
When I run this, I get the error:
status-check.sh: Syntax error: "fi" unexpected
This error is pointing to the nested fi on that second to last line. I am having trouble finding the syntax error here. Please assist! Thank you.
It's because there are no commands in the last else block. If you either remove this block or put a command in there (not a comment) you won't get a syntax error.
I have the following script in my spec file %preun tag:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
fi
I get "syntax error near unexpected token `fi'" error
but when I add else block it runs fine:
if [ $1 -eq 0 ]; then
echo "Stopping blah service before uninstalling.."
. /etc/init.d/blahforever stop
echo "blah service stopped."
else
echo "I dont need else block"
fi
But I don't need an else block! How do I get rid of it?
PS: I tried removing the semi colon after the condition and still didn't help.
This is a shell syntax error in the line ". /etc/init.d/blahforever stop".
There is no file to be sourced (i.e. read into the current shell)
called "stop".
I wrote two scripts which try to do the same action in two different ways, but I get errors each time I run those. Kindly requesting your help to correct my scripts and to improve my knowledge as well. All I am trying to do the vps setup in a single script. Following two scripts are just a portion of it which get errors each time.
1) Script to set hostname through cpanel xml-api for a vps in openvz node
cat vpstest.sh
#/bin/bash
hostname_status=`curl -sku root:PASSWORDHERE "https://ip.x.x.x:2087/xml-api/sethostname?hostname=server.domain.com" | awk -F"[<>]" '/status/{print $3}' | head -n1`
if [ $hostname_status -eq 1 ]; then
echo "Hostname set"
else
echo "Failed setting hostname"
fi
Output:
# ./vpstest.sh
./vpstest.sh: line 3: [: -eq: unary operator expected
Failed setting hostname
2) Script to set hostname via command line in an openvz node
cat vpstest1.sh
#!/bin/bash
hostname_status=`vzctl set containerID --hostname server.domain.com --save`
if [ "$hostname_status" -eq 1 ] ; then
echo "Hostname set"
else
echo "Failed setting hostname"
fi
Output:
# ./vpstest1.sh
./vpstest1.sh: line 3: [: CT configuration saved to /etc/vz/conf/containerID.conf: integer expression expected
Failed setting hostname
Can someone help to clear these errors?
First, the output of vzctl set containerID --hostname server.domain.com --save seems not be an integer value whereas -eq is only provided to do comparisons between integers values.
This would explain the following error :
integer expression expected
Then, you should read this reminder about the necessity (or not) to protect your variables with double quotes.
This would explain the following error, you could use something like :
./vpstest.sh: line 3: [: -eq: unary operator expected
If you want to check the status of a command :
command >/dev/null 2>&1 && echo "success" || echo "fail"
# or
if command >/dev/null 2>&1; then
echo "success"
else
echo "fail"
fi
You could also check the variable $? which correspond to the status of the previous command (0 when that command success or another integer value which is 1 in most of cases, or more).