1

i'm trying to make a shell script file that makes my friends life easier. My code is for starting up my servers when they want to. All they have to do is tell which game server they want and what version. That's where the problem begins. Whenever i fill in the game version, it makes a syntax error and gives me this (i'm the one typing mc and 1.17):

Which game server would you like to start? : mc
Game : Minecraft

Which version do you want it to be? : 1.17
./Game.sh: line 19: ((: 1.17 == 1.17 : syntax error: invalid arithmetic operator (error token is ".17 == 1.17 ")
./Game.sh: line 32: ((: 1.17 == 1.18 : syntax error: invalid arithmetic operator (error token is ".17 == 1.18 ")
./Game.sh: line 45: ((: 1.17 == 1.19 : syntax error: invalid arithmetic operator (error token is ".17 == 1.19 ")
Game : Terraria
[screen is terminating]
[screen is terminating]

And i don't know why this happens, i tried fixing it with making values called VAR1, VAR2, VAR3 with outputs being equal to 'version 1.17/1.18/1.19', but it still failed and gave this. Does anyone know why and how to fix this? Here's my code.

#! /bin/bash

# Clear the field
clear
# Begin values

# Recognise the game
read -p "Which game server would you like to start? :" game

# Minecraft
if (( $game == "MC")) || (( $game == "mc" )) || (( $game == "Minecraft" )) || (( $game == "minecraft" )) || (( $game == "MINECRAFT" )) || (( $game == "MineCraft" ))
then
  echo "Game : Minecraft "
  sleep 1
  clear
  read -p "Which version do you want it to be? :" version

  # 1.17
  if (( $version == "1.17" ))
  then
    sleep 1
    clear
    echo "1.17 is starting, good luck!"
    sleep 2
    screen -S "Minecraft 1.17" -d -m
    screen -S -X stuff "cd /home/joshsyx/Minecraft/1.17^M"
    screen -S -X stuff "java -Xmx6144M -Xms1024M -jar server.jar nogui^M"
    exit 0
  fi
   
  # 1.18
  if (( $version == "1.18" ))
  then
    sleep 1
    clear
    echo "1.18 is starting, good luck!"
    sleep 2
    screen -S "Minecraft 1.18" -d -m
    screen -S -X stuff "cd /home/joshsyx/Minecraft/1.18^M"
    screen -S -X stuff "java -Xmx6144M -Xms1024M -jar server.jar nogui^M"
    exit 0
  fi

  # 1.19
  if (( $version == "1.19" ))
  then
    sleep 1
    clear
    echo "1.19 is starting, good luck!"
    sleep 2
    screen -S "Minecraft 1.19" -d -m
    screen -S -X stuff "cd /home/joshsyx/Minecraft/1.19^M"
    screen -S -X stuff "java -Xmx6144M -Xms1024M -jar server.jar nogui^M"
    exit 0
  fi

  # Version not installed
  else
    echo "This version is not installed or you didn't put in a valid value. Please choose between 1.17, 1.18 or 1.19"
    exit 1
fi

# Terraria
if (( $game == "Terraria" )) || (( $game == "terraria" )) || (( $game == "TERRARIA" ))
then
  echo " Game : Terraria"
  screen -S "Terraria" -d -m
  screen -S -X stuff "cd /home/joshsyx/1444/Linux^M"
  screen -S -X stuff "./TerrariaServer.bin.x86_64^M"
  exit 0
fi

# Doesn't recognise
if (( $game != "MC")) || (( $game != "mc" )) || (( $game != "Minecraft" )) || (( $game != "minecraft" )) || (( $game != "MINECRAFT" )) || (( $game != "MineCraft" )) || (( $game != "mc" )) || (( $game != "Terraria" )) || (( $game != "terraria" )) || (( $game != "TERRARIA" ))
  echo "I do not recognise this game."
  exit 1
JoshSyx
  • 11
  • 1
  • 4
    Bash only does integer math, you can't compare floats. See [here](https://stackoverflow.com/questions/12722095/how-do-i-use-floating-point-arithmetic-in-bash). Try comparing as strings instead, e.g. `[[ "$version" = "1.17" ]]` – tjm3772 Oct 07 '22 at 16:59
  • 1
    If you really had to compare values as floating point numbers for some reason you'd need to delegate to an external tool like `awk` or `perl` that can handle that. – tjm3772 Oct 07 '22 at 17:00
  • You probably want string comparison rather than numeric anyway, since e.g. "1.1" and "1.10" are different version numbers, but when treated as numbers they're equal. If you *do* want floating-point numeric comparison, see ["How can I compare two floating point numbers in Bash?"](https://stackoverflow.com/questions/8654051/how-can-i-compare-two-floating-point-numbers-in-bash) – Gordon Davisson Oct 07 '22 at 21:28

0 Answers0