0

I am trying to write a script to automate the pulling, rendering and pushing of a LaTeX document from/to a repo. The pulling/pushing is no problem, but in order to begin the rendering I would like to check if .tex files have changed since the last update (variable oldHash holds the info). The problem is that when I execute the code, the counter variable gets reset to 0 once the loop exits. Here is the code:

#!/bin/bash

##### PULL FROM THE REPO AND SAVE THE HASH #####

# Get the diff between the two commits
diffFiles=$(git diff --name-only "$oldHash" HEAD@{0})

# The commmand produces the following output:
# Chapters/Chapter6.tex
# Other/CustomEnvironments.tex
# RawMaterial/LinearAlgebraNotes_4.pdf
# buildAll.sh
# main.tex

numberOfTexFiles=0

# Loop through every line of the output and strip the files from their extension
printf %s "$diffFiles" | while IFS= read -r line
do
    baseName=$(basename "$line")
    extension="${baseName##*.}"
    if [[ "$extension" == 'tex' ]]; then
        # If the file is a Tex, update the counter
        numberOfTexFiles=$(($numberOfTexFiles+1))
        echo $numberOfTexFiles
    fi
done

echo $numberOfTexFiles

if [ $numberOfTexFiles -gt 0 ]; then
    echo "FILES HAVE CHANGED"
    # Run some other stuff (namely pdflatex)
else
    echo "NOTHING CHANGED"
fi

What now happens is that the last if statements evaluate to false, and the pdflatex command never gets called. How can I fix it?

EDIT

As suggested by JID, the redirect was all that I needed. This is my solution:

while IFS= read -r line
do
    baseName=$(basename "$line")
    extension="${baseName##*.}"
    if [[ "$extension" == 'tex' ]]; then
        # If the file is a Tex, update the counter
        numberOfTexFiles=$(($numberOfTexFiles+1))
        echo $numberOfTexFiles
    fi
done <<< $(printf %s "$diffFiles")
Community
  • 1
  • 1
ruben1691
  • 353
  • 3
  • 20
  • don't pipe into a while loop use `done < (command)` instead of `command | while` . This has been asked countless times. search `bash while loop scope` in google for more info. –  Apr 28 '15 at 12:57

1 Answers1

-1
if [[ "$extension" == 'tex' ]]; 

My guess is semicolon at the end of if might be causing this issue. Could you please remove that and see if it works?

Kislay Kishore
  • 154
  • 3
  • 10