diff --git a/lib/shell/run_stata.sh b/lib/shell/run_stata.sh index dc04d30..1159ff0 100644 --- a/lib/shell/run_stata.sh +++ b/lib/shell/run_stata.sh @@ -5,26 +5,69 @@ run_stata() { # get arguments program="$1" logfile="$2" + OUTPUT_DIR=$(dirname "$logfile") programname="${program%.*}" # set Stata command if unset if [ -z "$stataCmd" ]; then - echo "No Stata command set. Using default: StataMP" + echo -e "\nNo Stata command set. Using default: StataMP" stataCmd="StataMP" fi - # run program, add output to logfile - echo "Executing: ${stataCmd} ${program}" - ( - ${stataCmd} -b -e do ${program} 1>> "${logfile}" 2>> "${logfile}" + # check if the command exists before running, log error if does not + if ! command -v ${stataCmd} &> /dev/null; then + error_time=$(date '+%Y-%m-%d %H:%M:%S') + echo -e "\033[0;31mProgram error\033[0m at ${error_time}: ${stataCmd} not found. Make sure command line usage is properly set up." + echo "Program Error at ${error_time}: ${stataCmd} not found." >> "${logfile}" + exit 1 # exit early with an error code + fi + + # capture the content of output folder before running the script + files_before=$(ls -1 "$OUTPUT_DIR" | grep -v "make.log") + + # log start time for the script + echo -e "\nScript ${program} in ${stataCmd} started at $(date '+%Y-%m-%d %H:%M:%S')" | tee -a "${logfile}" + + # run command and capture both stdout and stderr in log file + ${stataCmd} -e do "${program}" >> "${logfile}" 2>&1 + + # parse the log file to check for Stata error codes like `r(error)` + if egrep --before-context=1 --max-count=1 "^r\([0-9]+\);" "${programname}.log" > /dev/null 2>&1; then + return_code=1 # set the return code to 1 if any errors were found + fi + + # capture the content of output folder after running the script + files_after=$(ls -1 "$OUTPUT_DIR" | grep -v "make.log") - # report on errors - if [ $? -ne 0 ]; then - echo "Error: ${program} failed with exit code $return_code" | tee -a "${logfile}" + # determine the new files that were created + created_files=$(comm -13 <(echo "$files_before") <(echo "$files_after")) + + # report on errors or success and display the output + if [ "${return_code:-0}" -ne 0 ]; then + error_time=$(date '+%Y-%m-%d %H:%M:%S') + echo -e "\033[0;31mWarning\033[0m: ${program} failed at ${error_time}. Check log for details." # display error warning in terminal + echo "Error in ${program} at ${error_time}:" >> "${logfile}" # log error output + # add default log to log file and then delete default log + cat "${programname}.log" >> "${logfile}" + rm "${programname}.log" + + if [ -n "$created_files" ]; then + echo -e "\033[0;31mWarning\033[0m: there was an error, but files where created. Check log." + echo -e "\nWarning: There was an error, but these files were created: $created_files" >> "${logfile}" # log created files fi - ) + exit 1 + else + echo "Script ${program} finished successfully at $(date '+%Y-%m-%d %H:%M:%S')" | tee -a "${logfile}" + echo "Output:" >> "${logfile}" # log output + + # add default log to log file and then delete default log + cat "${programname}.log" >> "${logfile}" + rm "${programname}.log" + + if [ -n "$created_files" ]; then + echo -e "\nThe following files were created in ${program}:" >> "${logfile}" + echo "$created_files" >> "${logfile}" # list files in output folder + fi + fi - # add default log to log file and then delete default log - cat "${programname}.log" >> "${logfile}" - rm "${programname}.log" -} +} \ No newline at end of file