Skip to content

Commit

Permalink
#18 changed error handling in run_latex.sh
Browse files Browse the repository at this point in the history
  • Loading branch information
lucamlouzada committed Sep 18, 2024
1 parent 182cce5 commit efc8d95
Showing 1 changed file with 56 additions and 6 deletions.
62 changes: 56 additions & 6 deletions lib/shell/run_latex.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ run_latex() {
# Set output directory
# (If no third argument is provided, use the default)
if [ -n "$3" ]; then
outputdir="$3"
OUTPUT_DIR="$3"
else
outputdir="../output"
OUTPUT_DIR="../output"
fi

# Clean up
cleanup() {
mv "${programname}.pdf" ${outputdir}
if [ -f "${programname}.pdf" ]; then
mv "${programname}.pdf" "${OUTPUT_DIR}"
fi
rm -f "${programname}.aux" \
"${programname}.bbl" \
"${programname}.blg" \
Expand All @@ -35,9 +37,57 @@ run_latex() {
# Ensure cleanup is called on exit
trap 'cleanup' EXIT


echo "Executing: latexmk ${programname}.tex -pdf -bibtex"
(latexmk "${programname}.tex" -pdf -bibtex >> "${logfile}" 2>&1)
# Check if latexmk command exists
if ! command -v latexmk &> /dev/null; then
error_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "\033[0;31mProgram error\033[0m at ${error_time}: latexmk not found. Ensure LaTeX is installed."
echo "Program Error at ${error_time}: latexmk not found." >> "${logfile}"
return 1
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 ${programname}.tex in latexmk -pdf -bibtex started at $(date '+%Y-%m-%d %H:%M:%S')" | tee -a "${logfile}"

# run command and capture both stdout and stderr in the output variable
output=$(latexmk -interaction=nonstopmode -f "${programname}.tex" -pdf -bibtex 2>&1)
return_code=$? # capture the exit status

# perform cleanup
cleanup

# capture the content of output folder after running the script
files_after=$(ls -1 "$OUTPUT_DIR" | grep -v "make.log")

# 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 -ne 0 ]; then
error_time=$(date '+%Y-%m-%d %H:%M:%S')
echo -e "\033[0;31mWarning\033[0m: ${programname}.tex failed at ${error_time}. Check log for details." # display error warning in terminal
echo "Error in ${programname}.tex at ${error_time}: $output" >> "${logfile}" # log error output
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 ${programname}.tex finished successfully at $(date '+%Y-%m-%d %H:%M:%S')" | tee -a "${logfile}"
echo "Output: $output" >> "${logfile}" # log output

if [ -n "$created_files" ]; then
echo -e "\nThe following files were created in ${programname}.tex:" >> "${logfile}"
echo "$created_files" >> "${logfile}" # list files in output folder
fi

if [ ! -f "${OUTPUT_DIR}/${programname}.pdf" ]; then
echo -e "\033[0;31mWarning\033[0m: No PDF file was created. Check output in log."
echo -e "\nWarning: No PDF file was created. Check output above." >> "${logfile}" # log warning
fi
fi

}

0 comments on commit efc8d95

Please sign in to comment.