I'm an experienced programmer that is new to shell scripting. I'm putting together a shell script that erases all files of a certain age. These files are in directories provided on lines of a file that is provided via command line argument and takes any errors(permissions, etc) and emails them to a user. If someone could help me clean up my syntax it would be greatly appreciated.
My idea of what the script should look like: (assuming $file and $days passed in from command line)
foreach($line of $file)
find $line -mtime +$days -exec r -f{}\; > errors.txt
x=(wc -l < errors.txt)
if(x>0)
mail -s "errors" someone@email.com < errors.txt
fi
#!/bin/ksh
# or bash
# cleanscript
# arg1 = file, arg2= days
[ $# != 2 ] && echo "usage:$0 dirlistfile days" >&2 && exit 1
file="$1"
days="$2"
cat "$file" | while read line
do
find $line -mtime +$days -exec rm -f {} \; 2> errors.txt
# do cmd and put stdout to the var
x=$( wc -l < errors.txt )
# numeric testing builtin
if (( x>0 ))
# or using test or [ builtin command
# if [ "$x" -gt 0 ]
then
mail -s "errors" someone@email.com < errors.txt
fi
done