BASH script waiting when I need more

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 run some Minecraft servers that I own. As a result, I will add all the entries for other servers. This is the result 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 choose Creative + Survival, the same thing happens. Sorry if this is a stupid question, this is one of my first bash scripts.

Thanks!

+4
source share
4 answers

Missing bracket operator [ (test). It should be something like this:

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

NOTE. The spaces around these brackets are important. The square brackets around the conditional test are actually synonymous with the test statement. So the above is equivalent to:

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

But everyone uses brackets; I rarely see a script that uses the keyword test .

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

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

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


Update

Q: It seems to work, but it spits out an error code if I choose something other than "Creative + Survival". Is this supposed to happen?

A: It is not clear which error code spat out which component. I expect you to want to check every possible choice. You can do this with elif or with 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 ehco "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: Indentation is for readability only; bash takes care of newlines, not leading spaces.)

+2
source

In bash, you must remember that then must be on a new line. Unfortunately, your entire if incorrect. A proper string comparison looks something like this:

  if [[ "$server" = "pattern" ]]; then # do something else # do something else fi 
+3
source

Your if statement is incorrect.

  • The condition is in brackets: [[...]]
  • then goes to a new line
  • The statement ends with fi not end

There is also a Useless use of cat , which I corrected.

Try the following:

 server=$(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) if [[ $server == "Creative+Survival" ]] then gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh" else echo wrong answer... fi 
+2
source

Others answered syntax issues.

Go ahead, the case will serve you well:

 server=$(...) case $server in "Creative+Survival") gnome-terminal -x sh "/home/kulboy121/Servers/Creative and Survival/launch.sh" ;; "Tekkit Cheat Survival") # do something ;; "Flat Tekkit") # do something ;; "SunnyDale City") # do something ;; "Doom Dungeon") # do something ;; "Survival Tekkit") # do something ;; *) echo wrong answer ;; esac 

Also, instead of using zenity, use the bash select statement. Assuming you have bash v4

 declare -A port port["Creative+Survival"]=25565 port["Tekkit Cheat Survival"]=25566 port["Flat Tekkit"]=25567 port["SunnyDale City"]=25568 port["Doom Dungeon"]=25569 port["Survival Tekkit"]=25570 PS3="Select server: " select server in "${!port[@]}"; do if [[ -n $server ]]; then # user has given a valid selection echo do something with "$server" and "${port[$server]}" break fi done 
+1
source

All Articles