1

I have a scritp that gathers information about a subdirectory of files. I am checking that the time between file creation is uniform.

last=0
LOGCHECK="YES"
ls -l /dir/*.log | gawk '{print $8}' | sed s/:/*60+/g | bc |
        while read fname
        do
            current=$fname
            if [ $last = 0 ]; then
                last=$current
            elif [ $((current - last)) -ne 1 ]; then
                echo "Time difference discrepancy: $((current - last)) minute(s) is not expected"
                LOGCHECK="NO"
                last=$current    
            else
                last=$current
            fi      
        done

This outputs only if the time between .log file creation is not one minute. My problem is that the $LOGCHECK inside the while loop is in another subshell I believe from the pipe?

Is there a way to get this variable information?

1 Answer 1

3

This is a common situation with bash scripting. Restructure your loop like this:

while read fname
do
  # stuff
done < <(ls -l /dir/*.log | gawk '{print $8}' | sed s/:/*60+/g | bc)

Then variables set within the loop will still be available afterwards.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.