0

So I'm trying to make an automated bash script to help build certain versions of the Minecraft Spigot server. In order to "build" the server, you have to decompile the .jar file by running the following commands in the terminal.

export MAVEN_OPTS="-Xmx2G"

export JAVA_HOME="/usr/libexec/java_home -v 1.8" (Omit this line unless you are building a previous version then the current 1.13 build, which uses Java 11 and not Java 8)

java -jar BuildTools.jar (Optional: --rev flag can be used to dictate what version you would like to download of the server)

Now this is what I have come up with so far to make a simple "double click and done" bash script. I'm extremely new to running, compiling, writing, etc of bash scripts. The error I'm getting is something along the lines of "Can't find BuildTools.jar", however this is blocked by a pop up from my terminal that I can't get rid of and still see the message. Once I click ok, it just closes the window.

#!/bin/sh
cd "$( dirname "$0" )"

export MAVEN_OPTS="-Xmx2G"

export JAVA_HOME="/usr/libexec/java_home -v 1.8"

exec java -jar BuildTools.jar --rev 1.8.8

My main objective is to just be able to edit the script manually and dictate what version I would like to download by changing the --rev flag, and deleting the "java version" line if not using the current 1.13 build.

Ideally I would like the script once run, to ask me what version I would like to use. If i hit enter it omits the --rev flag & java version line, and downloads the current version (1.13), if I specify a version (i.e. "1.9") it runs the export JAVA_HOME="/usr/libexec/java_home -v 1.8" line and adds the --rev flag to the end of the exec line.

If this doesn't make any sense, here is the article explaining how to do what I'm doing through the terminal with no "automated script".

https://www.spigotmc.org/wiki/buildtools/

I would like to get this script to work as I would forward this script along to other server developers that would be able to use the script in their every day development and not have to run multiple commands through the terminal every time they want to build a server.

l0b0
  • 55,365
  • 30
  • 138
  • 223
J. Robinson
  • 931
  • 4
  • 17
  • 45
  • 1
    This may not be the whole problem, but you certainly need to change the line that defines `JAVA_HOME` to something like `export JAVA_HOME="$(/usr/libexec/java_home -v 1.8)"` -- the way you have it, you're setting the variable to that string, rather than running it as a command and setting the variable to the command's output. – Gordon Davisson Mar 07 '19 at 04:26

1 Answers1

2

The easiest way to do something like this is to take a single optional argument:

java -jar BuildTools.jar --rev "${1-1.8.8}"

This may look a bit clunky, but it means the script can be called like my_script.sh to use the default version or my_script.sh 2.0 to use version 2.0.

Another simple improvement would be to pull out a variable for the default version:

default_version='1.8.8'
java -jar BuildTools.jar --rev "${1-$default_version}"

Your shebang line uses /bin/sh, which is not the same as Bash. This may make your script very slightly more portable than using Bash, but Bash is by now very common, and you can take advantage of Bash features in your script. For example:

  • You can declare the default version variable constant with readonly default_version.
  • You can get the script path more reliably.
l0b0
  • 55,365
  • 30
  • 138
  • 223