-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript-wrapper.sh
executable file
·34 lines (26 loc) · 1.1 KB
/
script-wrapper.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/bash
set -euo pipefail
script="${1:-}"
shift
test -f "$script" || { echo "$script does not exist"; exit 1; }
# batch mode writes output for script.do to script.log in PWD, so we preserve that
# behaviour
script_name=$(basename "$script")
log=${script_name%.do}.log
# Stata is super odd in its cli interactions and behaviour. It will never exit
# with anything except code 0. It does stop on error however, but only when
# running a script. So we wrap the actual script with a wrapper that runs the
# original script, and then writes that has succeeded to a file. Because we are
# running a script, if there is an error, it will stop, and not write the file,
# so we can use this as a proxy for success or failure.
success=$(mktemp "/tmp/$script_name.XXXX.out")
wrapper=$(mktemp "/tmp/$script_name.XXXX.do")
cat <<EOF > "$wrapper"
. do "$script" $@
. file open output using "$success", write text replace
. file write output "success"
. file close output
EOF
/usr/local/bin/stata "$wrapper" "$@" < /dev/null | tee "$log"
# exit cleanly if we find the file has been written
grep -q success "$success" 2>/dev/null