Bookmark and Share

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

[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 <>
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.,


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  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"


    stata -b do filename\ with\

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


if [ "$1" = "do" ] && [ "$args" -gt 1 ]
    log="`basename -s .do "$2"`.log"
    # mimic Stata's behavior (stata -b do "foo" -> 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" ]
    log="`basename -s .do "$1"`.log"
    log=${log/% */.log}
    # else Stata interprets it as a command and logs to stata.log

# in batch mode, nothing sent to stdout (is this guaranteed?)
stderr=`stata-se -b $cmd "$@" 2>&1`
if [ -n "$stderr" ]  # typically usage info
    echo "$stderr"
    exit $rc
elif [ $rc != "0" ]
    exit $rc
    # 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"
        exit 1

*   For searches and help try:

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