Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Stata 11 v Stata 12: difference in batch mode behaviour


From   Phil Schumm <[email protected]>
To   [email protected]
Subject   Re: st: Stata 11 v Stata 12: difference in batch mode behaviour
Date   Wed, 5 Oct 2011 18:33:45 -0500

On Oct 5, 2011, at 1:58 PM, Billy Schwartz wrote:
> Just out of curiosity, care to share your wrapper? Is it a bash script?


Of course (see below my signature), with three caveats.  First, based on James' explanation, I now understand that

    stata -b <do-file>

was never intended to be legitimate syntax, and given this, I will likely modify the script so as to no longer permit this.  I had been thinking about this as, for example, the way you use Python, e.g.,

    python

to start the (interactive) interpreter, and

    python <script>

to execute the file <script>.  However, since adding the "do" is easy, I see no reason to justify a wrapper that has syntax different from that of the Stata executable itself.

Second, this script is a rewritten version of script posted by Brendan Halpin on his blog at http://teaching.sociology.ul.ie/bhalpin/wordpress/?p=122.  He deserves the credit for the idea, and you might want to look at how his script differs from mine in order to figure out what works best for you.

Finally, you'll see that I tried to mimic the behavior of the Stata executable when handling filenames with spaces, e.g., neither

    stata -b do "filename with spaces.do"

nor

    stata -b do filename\ with\ spaces.do

work.  I didn't think carefully about this at the time, and in retrospect, I would probably handle this differently if I were doing it again (none of my filenames have spaces in them, so this never bites me).  Anyway, don't waste any time taking my approach to this issue seriously.

Of course, comments/suggestions are welcome.


-- Phil

#! /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=$#

cmd=""
if [ "$1" = "do" ] && [ "$args" -gt 1 ]
then
    log="`basename -s .do "$2"`.log"
    # mimic Stata's behavior (stata -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


*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index