Skip to main content
Revision of logic error noted by OP, and improvement of the explanation of the while loop.
Source Link
Jim L.
  • 8.8k
  • 1
  • 15
  • 29

Distilling @Freddy's solution slightly:

#!/bin/bash

while ! find /home -maxdepth 1 -type f -mmin -1 -name 'test.log' | grep -q .
do
    echo "Now sleeping for 20 seconds"
    sleep 20
done
echo "Job is finished"

This solution uses grep's -q switch to suppress the usual output of grep, sowhich simply tests the output of find for an empty/not empty condition. The while loop will continue to loop so long as the grepfind command failsoutputs at least one line of text. As soon as Once the findtest.log produces some outputfile is older than one minute, the grepfind command will succeedno longer produces any output, and the negated whilegrep condition will be falsecommand fails, causing the loop to terminate.

Distilling @Freddy's solution slightly:

#!/bin/bash

while ! find /home -maxdepth 1 -type f -mmin -1 -name 'test.log' | grep -q .
do
    echo "Now sleeping for 20 seconds"
    sleep 20
done
echo "Job is finished"

This solution uses grep's -q switch to suppress the usual output of grep, so the while loop will continue to loop so long as the grep command fails. As soon as find produces some output, the grep command will succeed, and the negated while condition will be false, causing the loop to terminate.

Distilling @Freddy's solution slightly:

#!/bin/bash

while find /home -maxdepth 1 -type f -mmin -1 -name 'test.log' | grep -q .
do
    echo "Now sleeping for 20 seconds"
    sleep 20
done
echo "Job is finished"

This solution uses grep's -q switch to suppress the usual output of grep, which simply tests the output of find for an empty/not empty condition. The while loop will continue to loop so long as the find command outputs at least one line of text. Once the test.log file is older than one minute, find no longer produces any output, and the grep command fails, causing the loop to terminate.

Source Link
Jim L.
  • 8.8k
  • 1
  • 15
  • 29

Distilling @Freddy's solution slightly:

#!/bin/bash

while ! find /home -maxdepth 1 -type f -mmin -1 -name 'test.log' | grep -q .
do
    echo "Now sleeping for 20 seconds"
    sleep 20
done
echo "Job is finished"

This solution uses grep's -q switch to suppress the usual output of grep, so the while loop will continue to loop so long as the grep command fails. As soon as find produces some output, the grep command will succeed, and the negated while condition will be false, causing the loop to terminate.