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.
Related
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 have been trying to make a shell script in bash that will display the following:
You are the super user (When I run the script as root).
You are the user: "user" (When I run the script as a user).
#!/bin/bash/
if { whoami | grep "root" }; then
echo $USER1
else
echo $USER2
fi
I keep recieving these syntax error messages:
script.sh: line 2: syntax error near unexpected token `then'
script.sh: line 2: `if { whoami | grep "root" }; then'
Could someone help me out?
If braces are used to chain commands then the last command must have a command separator after it.
{ foo ; bar ; }
userType="$(whoami)"
if [ "$userType" = "root" ]; then
echo "$USER1"
else
echo "$USER2"
fi
pay attention with your first line, the correct syntax for she-bang is:
#!/bin/bash
everything you put there, is the interpreter of your script, you can also put something like #!/usr/bin/python for python scripts, but your question is about the if statement, so you can do this in two ways in shell script using
if [ test ] ; then doSomething(); fi
or
if (( test )) ; then doSomething(); fi
so to answer your question basically you need to do this
#!/bin/bash
if [ `id -u` -eq 0 ] ; then
echo "you are root sir";
else
echo "you are a normal user"
fi
if (( "$USER" = "root" )); then
echo "you are root sir";
else
echo "you are a normal user"
fi
note that you could use a command using `cmd` or $(cmd) and compare using -eq (equal) or = (same), hope this help you :-)
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/.
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 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