Advertisement
Guest User

Untitled

a guest
Oct 20th, 2014
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Defining a few variables...
  5. #
  6. LATEXCMD="/usr/bin/pdflatex"
  7. LATEXDOC_DIR="/home/jaakko/Documents/LaTeX"
  8. MD5SUMS_FILE="$LATEXDOC_DIR/md5.sum"
  9.  
  10. #
  11. # This function checks whether a file needs to be updated,
  12. # and calls LATEXCMD if necessary. It is called for each
  13. # .tex file in LATEXDOC_DIR (see below the function).
  14. #
  15. update_file()
  16. {
  17. [[ $# -ne 1 ]] && return;
  18. [[ ! -r "$1" ]] && return;
  19.  
  20. # Old MD5 hash is in $MD5SUMS_FILE, let's get it.
  21. OLD_MD5=$(grep "$file" "$MD5SUMS_FILE" | awk '{print $1}')
  22.  
  23. # New MD5 hash is obtained through md5sum.
  24. NEW_MD5=$(md5sum "$file" | awk '{print $1}')
  25.  
  26. # If the two MD5 hashes are different, then the files changed.
  27. if [ "$OLD_MD5" != "$NEW_MD5" ]; then
  28. echo "$LATEXCMD" -output-directory $(dirname "$file") "$file"
  29.  
  30. # Calling the compiler.
  31. "$LATEXCMD" -output-directory $(dirname "$file") "$file" > /dev/null
  32. LTX=$?
  33.  
  34. # There was no "old MD5", the file is new. Add its hash to $MD5SUMS_FILE.
  35. if [ -z "$OLD_MD5" ]; then
  36. echo "$NEW_MD5 $file" >> "$MD5SUMS_FILE"
  37. # There was an "old MD5", let's use sed to replace it.
  38. elif [ $LTX -eq 0 ]; then
  39. sed "s|^.*b$OLD_MD5b.*$|$NEW_MD5 $file|" "$MD5SUMS_FILE" -i
  40. fi
  41. fi
  42. }
  43.  
  44. # Create the MD5 hashes file.
  45. [[ ! -f "$MD5SUMS_FILE" ]] && touch "$MD5SUMS_FILE"
  46.  
  47. IFS='n'
  48. find "$LATEXDOC_DIR" -iname "*.tex" | while read file; do
  49. # For each .tex file, call update_file.
  50. update_file $file
  51. done
  52.  
  53. $ watch /path/to/script.sh
  54.  
  55. $ watch -n 2 /path/to/script.sh
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement