Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BASH script expecting then, when I need else

I need a little help with my bash script:

#!/bin/bash

zenity --list --title="Select Server" --text="Select the server to start" --radiolist \
--column= --column=Server --column=Port \
FALSE "Creative+Survival" 25565 \
FALSE "Tekkit Cheat Survival" 25566 \
FALSE "Flat Tekkit" 25567 \
FALSE "SunnyDale City" 25568 \
FALSE "Doom Dungeon" 25569 \
FALSE "Survival Tekkit" 25570 \
| cat > ~/temp


server=$(cat ~/temp)

if $server="Creative+Survival" then
gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh"
end

else
echo wrong answer...
end

rm ~/temp

This is a script to launch some Minecraft servers I own. I will eventually add if entries for all the other servers as well. This is the output when I do not select Creative+Survival:

Server Startup - INCOMPLETE.sh: 20: Server Startup - INCOMPLETE.sh: Syntax error: "else" unexpected (expecting "then")

And when I do select Creative+Survival, the same thing happens. Sorry if this is a stupid question, this is one of my first bash scripts.

Thank you!

like image 892
JonaK Avatar asked Jan 14 '23 15:01

JonaK


1 Answers

The bracket [ (test) operator is missing. It should be something like this:

if [ "$server" = "Creative+Survival" ]
then
    echo "true"
else
    echo "false"
fi

NOTE: the spaces around those brackets are important. The square brackets around the conditional test are actually a synonym for the test operator. So the above is equivalent to:

if test "$server" = "Creative+Survival"
then
   echo "true"
else
   echo "false"
fi

But everyone uses the brackets; I rarely see scripts that use the test keyword.

Also allowed in the bash shell (although this is not as portable because it is not a POSIX standard) is to use double brackets:

if [[ "$server" = "Creative+Survival" ]]
then

Here's a link to page that describes the differences between [, [[, and test: http://mywiki.wooledge.org/BashFAQ/031


Update

Q: This seems to work, but it spits out an error code if I select anything other then "Creative+Survival". Is this supposed to happen?

A: It's not at all clear what error code is being spit out by what component. I expect you want to check for each possible selection. You can do that with an elif, or with a case.

if [ "$server" = "Creative+Survival" ]
then
    echo "Creative and Survival"
elif [ "$server" = "Tekkit Cheat Survival" ]
then
    echo "Tekkit Cheat Survival"
elif [ "$server" = "Flat Tekkit" ]
then
    echo "Flat Tekkit"
else
    echo "no action for specified server"
fi


case "$server" in
    'Creative+Survival')
        echo "Creative and Survival"
        ;;
    'Tekkit Cheat Survival')
        echo "Tekkit Cheat Survival"
        ;;
    *)
        echo "no action found for server $server"
        ;;
esac

(NOTE: the indentation is to improve readability only; bash cares about the newlines, not the leading spaces.)

like image 109
spencer7593 Avatar answered Jan 20 '23 16:01

spencer7593