-
-
Save johnghill/613097ad4b84d25d5c29 to your computer and use it in GitHub Desktop.
[stata] [make] Wrapper to execute Stata batch files on Linux (stata -b), greps for do file errors and passes appropriate exit code to e.g. make
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/sh | |
# Author: Brendan Halpin | |
# Forked from: http://teaching.sociology.ul.ie/bhalpin/wordpress/?p=122 | |
# Nov 7 2001 21:05:17 | |
# A wrapper for running Stata in batch mode. | |
# Main purpose is to catch errors and pass them to the calling | |
# process, typically "make". To do this it catches a couple of | |
# typical problems with the do-file not existing etc, and otherwise | |
# runs Stata (under nice), directing the output to $1.log. It then | |
# greps the log file for error messages, and returns an error if it | |
# finds them. grep should provide enough context that you can see | |
# the error message on stdout as well. | |
# It additionally appends information about wall and cpu time to | |
# the logfile, along with a time stamp. | |
############################################################################## | |
## Changelog # | |
############################################################################## | |
## John Hill | |
## Dec 4 2015 22:34:12 | |
# Removed invisible characters created by WordPress verbatim environment | |
# Added \newlines to bash echo to create visual pairs | |
# Colour-coded exit messages for ease-of-use: green=exit==0, red=exit==1 | |
# See: http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux | |
progname=`basename $0` | |
# Strip the .do if it is there, stata ignores it | |
statacode=${1%*.do} | |
# Test for do-file in another directory -- Stata logs to current | |
# directory in either case, so direct extra log-lines to correct | |
# location | |
statacodestripdir=${statacode##*/} | |
statalog=$statacodestripdir.log | |
if [ $statacode != $statacodestripdir ]; then | |
echo "$progname: Note: do-file may not be in current directory, but log-file is"; | |
fi | |
echo "$progname: Running Stata on $statacode..." | |
#if [ "$2" == "" ]; then | |
# arg2="-m200" | |
# else arg2=$2 | |
#fi | |
# Test for the existence of the do-file | |
if [ -r ${statacode}.do ]; then | |
echo "$progname: Starting: `date`" >> $statalog | |
nice time -f "$progname: Elapsed: %E; System: %S; User: %U; Major PFs: %F" stata -b do $statacode 2> /tmp/stb$$timelog | |
exitcode=$? | |
#cat /tmp/stb$$timelog | |
cat /tmp/stb$$timelog >> $statalog | |
echo "$progname: Finished: `date`" >> $statalog | |
rm -f /tmp/stb$$timelog | |
if [ "$exitcode" != "0" ]; then | |
echo "\033[0;31mStata exiting with exit code $exitcode\033[0m" && exit $exitcode | |
fi | |
if (egrep --before-context=1 "^r\([0-9]+\)" $statacode.log); then | |
echo "\033[0;31m$progname: Stata errors found in $statacode.do\n\033[0m"; | |
exit 1; | |
else echo "\033[0;32m$progname: No Stata errors found\n\033[0m"; | |
fi | |
else | |
echo "$progname: ${statacode}.do does not exist"; | |
exit 1; | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Substitute line 57 to provide compatibility with Stata's official unofficial fix for the eternal missing shared libraries bug on Linux:
nice time -f "$progname: Elapsed: %E; System: %S; User: %U; Major PFs: %F" /usr/local/stata15/run-stata c -b do $statacode 2> /tmp/stb$$timelog