Reputation:
I am trying to write a shell script that will search for a file name (given as an argument) and if the file was modified less than or equal to 10 minutes ago, exit 0, if it was modified more than 10 minutes ago exit 1, and if the file doesn't exist exit 2.
Here's my code:
if find $1
then
if find $1 -mmin -11
then
echo "Exit 0"
else
echo "Exit 1"
fi
else
echo "Exit 2"
fi
How do I make this search through ALL files on the system?
Also, if the file exists then check if it was modified within the past 10 minutes, if it was then exit 1. If the file doesn't exist then exit 2. I have used echo "" so that I can see which exit happens.
Upvotes: 2
Views: 310
Reputation: 212654
What do you want to do if there are two files of that name found on the filesystem? (For the moment, I'll assume that is not an issue and only work with the first file found.) Your question contradicts itself: in the preamble you say you want to exit with 0 if it has been modified in the last 10 minutes, but later you say that a recent modification should trigger an exit of 1. The following solution returns 0 of the file has NOT been modified in the last 10 minutes. This requires the gnu extension to date that gives the %s format specifier.
#!/bin/sh fullpath=$( find / -name ${1?No file specified} | sed 1q | grep . ) || exit 2 test $( expr $( date +%s ) - $( stat -c %Y $fullpath )) -gt 600
Upvotes: 0
Reputation: 15266
if [[ -n $1 ]]; then
find / -name $1 -mmtime -10 2>/dev/null
if [[ $? == 0 ]]; then
exit 0
else
exit 1
fi
else
...
Upvotes: 1