Skip to content

Instantly share code, notes, and snippets.

@xtman
Created February 26, 2013 01:07
Show Gist options
  • Save xtman/5034870 to your computer and use it in GitHub Desktop.
Save xtman/5034870 to your computer and use it in GitHub Desktop.
A wrapper shell script that executes a matlab script or command.
#!/bin/sh
#
# Locate matlab binary executable file.
#
MATLAB=$(which matlab)
if [ -z ${MATLAB} ]; then
if [ -n ${MATLAB_HOME} ]; then
MATLAB=${MATLAB_HOME}/bin/matlab
fi
fi
if [ -z ${MATLAB} ] || [ ! -f ${MATLAB} ]; then
echo "Error: could not find matlab binary file." 1>&2
exit 1
fi
#
# Initialize the stdout file of the matlab execution.
#
NOW=$(date +%Y%m%d%H%M%S)
OUTPUT=$(mktemp /tmp/matlab.out.${NOW}.XXXXXX)
SENTINEL=${OUTPUT}
#
# Execute the matlab script/command specified in $@
#
${MATLAB} -nodisplay -nojvm -nosplash -r "$@"";disp('${SENTINEL}');exit;" </dev/null 2>&1 | tee ${OUTPUT} | grep -v ${SENTINEL}
#
# Check if the sentinal sign is included in the output.
# If sentinal sign is included, means the execution is completed successfully, exit with code 0.
# If sentinal sign is not included, means the execution is not incomplete, exit with code 1.
if [ -n $(tail -n 2 ${OUTPUT} | grep ${SENTINEL}) ]; then
rm -f ${OUTPUT}
exit 0
else
echo "Error: matlab script/command failed to complete. See ${OUTPUT} for more detail." 1>&2
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment