Skip to content

Instantly share code, notes, and snippets.

@edvind
Last active September 9, 2021 09:46
Show Gist options
  • Save edvind/c1e0afbd40006f6183f3 to your computer and use it in GitHub Desktop.
Save edvind/c1e0afbd40006f6183f3 to your computer and use it in GitHub Desktop.
Simple install script for Minecraft server
#!/bin/bash
# Simple install script for Minecraft server
# Version 1.2
# Written by Joel Bergroth
#
# Usage ./install.sh "/full/path/to/desired/server/location"
if [ -z "$1" ]; then
read -p "Enter full server path: " -e serverpath
else
serverpath="$@"
fi
username=$(whoami)
if [ "$username" = "root" ]; then
read -p "Minecraft server user: " -e username_input
fi
[ -z "$username_input" ] || username=$username_input
printf "%s " "Newer versions of Minecraft requires that you accept the EULA by writing to a file called eula.txt provided by Mojang before you're able to start the server. Would you like to auto-accept the eula.txt? (Please note that you are required to read and accept the EULA first). [Y/n]"
read answer_eula
case $answer_eula in
[yY] | [yY][Ee][Ss] )
eula="Auto accept"
;;
[nN] | [n|N][O|o] )
eula="Do nothing"
;;
*) printf "%s\n" "Could not interpret answer, aborting."
exit 1
;;
esac
printf "\n%s\n" "----- Server settings -----"
printf "%s\n" "Location: $serverpath"
printf "%s\n" " User: $username"
printf "%s\n" " EULA: $eula"
printf "%s\n" "---------------------------"
printf "%s " "Is this information correct? [Y/n]"
read answer
case $answer in
[yY] | [yY][Ee][Ss] )
printf " * %s\n" "Starting installation"
;;
[nN] | [n|N][O|o] )
printf " * %s\n" "Aborting installation"
exit 1
;;
*)
printf " * %s\n" "Could not interpret answer, aborting."
exit 1
;;
esac
printf " * %s\n" "Creating $serverpath"
mkdir -p "$serverpath"
cd "$serverpath"
if [ "$eula" = "Auto accept" ]; then
printf " * %s\n" "Creating eula.txt"
printf "%s" "eula=true" > eula.txt
fi
versions_json=http://s3.amazonaws.com/Minecraft.Download/versions/versions.json
json_regex="\"(release)\": \"(.*)\""
printf " * %s" "Checking latest stable (release) version..."
while read line; do
if [[ $line =~ $json_regex ]]; then
release_version="${BASH_REMATCH[2]}"
break
fi
done <<<"$(wget -qO- $versions_json)"
printf " %s\n" "$release_version"
download_url="https://s3.amazonaws.com/Minecraft.Download/versions/$release_version/minecraft_server.$release_version.jar"
download_filename="minecraft_server.jar"
printf " * %s" "Downloading $download_filename..."
wget --progress=dot -O $download_filename $download_url 2>&1 | \
grep -oP --line-buffered "(\d{1,3})%" | \
while read line; do
printf "\r * %s %s" "Downloading $download_filename..." "$line"
done; test ${PIPESTATUS[0]} -eq 0 && printf "\r * %s %s\n" "Downloading $download_filename..." "100%" || printf "\r * %s %s\n" "Downloading $download_filename..." "failed"
printf " * %s\n" "Setting $username as owner of $serverpath"
chown -R $username:$username "$serverpath"
printf "%s\n" "---------------------------"
printf "%s\n" "Server successfully downloaded and installed to: $serverpath"
if [ -z "$(which java)" ]; then
printf "\n%s\n" "Note: You have to install java before you can use the server."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment