Output of find command to variable?

Hi,

I'd like to assign the output of the find command to a variable.

What I need is to run the find command, and if it returns zero files, the program exits.

so i'm trying to assign the output of the find command to the $var1 variable....and then if this is less than one, I echo a message and exit.

So far I have:

find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; > $var1
                 
                 if [ "$var1" -lt 1 ]
                 then
                    echo "No files found"
                    exit     
                else
                sleep 1
                fi

i'm getting [: : integer expression expected

var1=$( find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; | wc -l )
if [ $var1 -eq 0 ]
then
     echo "No files found"
     exit
else
     sleep 1
fi
1 Like

You showed us a script that isn't working, but you didn't really say what you wanted the script to do. The script bipinajith provided assumes that you just want to know if any files were found. I'm assuming that you have something generating log files, want to process the files found, and repeat the find after you've completed processing the files found by the 1st find until you have caught up with whatever is creating the log files. If that is what you want, try something like:

#!/bin/ksh
while [ $(find /apps//log -name 'tuesday*log' -mtime +1 -exec ls -lhrt {} \; |
                tee find_out.$$ | wc -l) -ne 0 ]
do      # process found files
        while IFS="" read -r ls_out
        do
                printf "Processing ls output line:\n\t%s\n" "$ls_out"
                # ... ... ...
        done < find_out.$$
        sleep 1
done
rm find_out.$$
echo "No files found"