lundi 21 décembre 2020

Bash script: elif triggered when it isn't meant to

I am certain this elif statement should not result to true and yet it executes.

start()
{
    echo start was called
}

stop()
{
    echo stop was called
}



if [ "$#" -eq 1 ] && [ "$1" = "start" ]
then
    stop
    start
elif [ "$#" -eq 1 ] && [ "$1" = "stop" ]
then
    stop
else
    echo USAGE: sudo ./bigip.sh start|stop
    exit
fi

echo the machine may need to reboot for changes to take effect

As you can see, I have two functions and want to call them based on the input argument given but when there are no arguments, more than one argument, or the first argument is neither stop or start, I want the usage to be printed.

When running the script with ./test.sh start or ./test.sh stop it behaves as expected but when I run it with no arguments, only the stop function is called making me believe that something is wrong with my elif statement

Aucun commentaire:

Enregistrer un commentaire