Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active November 23, 2019 23:27
Show Gist options
  • Save mavaddat/3a9fad318f79a35d92ca9c559bceaf22 to your computer and use it in GitHub Desktop.
Save mavaddat/3a9fad318f79a35d92ca9c559bceaf22 to your computer and use it in GitHub Desktop.
This implements a bash function (GNU Bash or Bourne-again shell) that compiles and runs a target java class (with attendant .java file). It assumes your target exists somewhere in $GITHUB_PATH
#!/bin/bash
readonly GITHUB_PATH=~/Documents/GitHub
function compileRun () {
r () {
if [ $1 -eq 0 ]
then #compiled successfully
java $2
return 0
else #compile failed
echo "\e[31mCouldn't compile, so we will not run \"java $2\"\e[0m" >&2 # TODO: this does not correctly set the terminal colour
return 1
fi
}
#TODO: from lines 15 to 18, this does not work
if [[${1} =~ ^[\S\s]+?\.java$]]; then #user included the .java extension in the target file name
set -- $(echo ${1} | sed -e 's/^([\S\s]+?)\.java/\1/g') "${@:2}"
# end TODO
if test -f "${1}.java"; then #this means that it finds the file you're trying to compile in the present directory
javac "${1}.java"
r $? $1
return $?
else #couldn't find your file, so it needs to search
a=$(find ${GITHUB_PATH} -name ${1}.java)
b=$(echo $a | sed -e 's/[^\/]*$//g')
cd "$b"
javac "$a"
r $? $1
return $?
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment