Skip to content

Instantly share code, notes, and snippets.

@korenmiklos
Forked from pschumm/statab
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korenmiklos/bd03c59bc614292ea176 to your computer and use it in GitHub Desktop.
Save korenmiklos/bd03c59bc614292ea176 to your computer and use it in GitHub Desktop.
#! /bin/bash
# Wrapper for "stata -b" which issues an informative error msg and appropriate
# (i.e., non-zero) return code
# The basic idea for this script (including grepping the log file to determine
# whether there was an error) was taken from a similar script posted by Brendan
# Halpin on his blog at http://teaching.sociology.ul.ie/bhalpin/wordpress/?p=122
args=$# # number of args
cmd=""
if [ "$1" = "do" ] && [ "$args" -gt 1 ]
then
log="`basename -s .do "$2"`.log"
# mimic Stata's behavior (stata-se -b do "foo bar.do" -> foo.log)
log=${log/% */.log}
# Stata requires explicit -do- command, but we relax this to permit just the
# name of a single do-file
elif [ "$args" -eq 1 ] && [ "${1##*.}" = "do" ] && [ "$1" != "do" ]
then
cmd="do"
log="`basename -s .do "$1"`.log"
log=${log/% */.log}
else
# else Stata interprets it as a command and logs to stata.log
log="stata.log"
fi
# in batch mode, nothing sent to stdout (is this guaranteed?)
stderr=`stata-se -b $cmd "$@" 2>&1`
rc=$?
if [ -n "$stderr" ] # typically usage info
then
echo "$stderr"
exit $rc
elif [ $rc != "0" ]
then
exit $rc
else
# use --max-count to avoid matching final line ("end of do-file") when
# do-file terminates with error
if egrep --before-context=1 --max-count=1 "^r\([0-9]+\);$" "$log"
then
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment