Skip to content

Instantly share code, notes, and snippets.

@tueda
Last active October 19, 2015 12:53
Show Gist options
  • Select an option

  • Save tueda/ec871fc6de19a4c8e525 to your computer and use it in GitHub Desktop.

Select an option

Save tueda/ec871fc6de19a4c8e525 to your computer and use it in GitHub Desktop.
A launcher for Mathematica programs. #bin #mma
#! /bin/sh
#
# @file math.sh
#
# A launcher for Mathematica programs.
#
# Example:
# math.sh variable=string-value file.m
#
set -e
prog=`basename "$0"`
# Prints the absolute path to the directory containing the given path.
# The directory must exist.
abs_dirname() {(
cd "`dirname \"$1\"`" && pwd
)}
# Options.
opt_math=math
opt_verbose=false
opt_run=
# Prints the usage help.
show_help() {
cat <<END
Usage: math.sh [-h|--help] [-I<dir>...] [--math=<math-executable>]
[-v|--verbose] [--] [<var>=<value>...] <file>...
END
}
# Parse arguments.
for opt_arg do
case $opt_arg in
-h|--help)
show_help
exit 0
;;
-I*)
opt_val=`expr "$opt_arg" : '-I\(.*\)' || :`
opt_run=$opt_run'PrependTo[$Path,"'`abs_dirname "$opt_val/dummy"`'"];'
;;
--math=*)
opt_math=`expr "$opt_arg" : '--[^=]*=\(.*\)' || :`
;;
-v|--verbose)
opt_verbose=:
;;
--)
shift
break
;;
-*)
echo "$prog: error: unknown option \"$opt_arg\"" >&2
exit 1
;;
*)
break
;;
esac
shift
done
for opt_arg do
case $opt_arg in
?*=*)
opt_var=`expr "$opt_arg" : '\([^=]*\)=.*' || :`
opt_val=`expr "$opt_arg" : '[^=]*=\(.*\)' || :`
opt_run=$opt_run"$opt_var=\"$opt_val\";"
;;
*)
break
;;
esac
shift
done
for opt_arg do
opt_run=$opt_run"Get[\"$opt_arg\"];"
done
# Run the Mathematica programs.
opt_run='PrependTo[$Path,"'`abs_dirname "$0"`'"];'$opt_run
opt_run=$opt_run'Quit[];'
if $opt_verbose; then
echo "$opt_math" -run "$opt_run"
fi
"$opt_math" -run "$opt_run"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment