Created
October 7, 2014 00:06
-
-
Save pschumm/b967dfc7f723507ac4be to your computer and use it in GitHub Desktop.
Wrapper for "stata -b" which issues an informative error msg and appropriate (i.e., non-zero) return code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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
I ended up finding it easier having a simple script
checklog.sh
:and then just running it after calling
stata -b
, like so:This is nice because it also supports reading the log from stdin. So if I'm running
dyntext
on a file, I'll do:and it'll still catch any errors.