All,
I am new to shell scripting and trying to get the count of files that starts with error and with extension .out, if the count is greater than 0 and zip the file and send an email with the content of error.out file, here is my script
cd /temp
testcount =$('find . -name '*.out' -print | wc -l')
echo $testcount
if [[ $testcount -ne 0 ]]
then
zip error_log.zip *.out
cat error_*.out | mailx -s "for testing" username@doamin.com
exit 1
else
exit 0
fi
when I tried to execute above script I am getting below error
./test.ksh: line 2: find . -name *.out -print | wc -l: command not found
./test.ksh: line 2: testcount: command not found
Can someone please tell me what is wrong with this script?
You have an extra single-quote in front of find, remove it.
cd /temp
testcount =$('find . -name '*.out' -exec wc -l {} \; | awk '{sum +=$1 } END {print sum} )
echo $testcount
if [[ $testcount -ne 0 ]]
then
zip error_log.zip *.out
cat error_*.out | mailx -s "for testing" username@doamin.com
exit 1
else
exit 0
fi
Jim, you're counting the lengths of the files when I think the OP just wants the number of them.
I think you can get away with not using find, too:
set -- error*.out
if [ -f "$1" ]
then
echo "$# files"
stuff to zip and email
else
echo "No files"
fi
Though set -- overwrites your $1 $2 ... parameters.
Corona,
I am not counting the length of file , I am counting the number of files.
Jim,
I tried your code..but getting same error.
can you give me the solution for this, it might resolve my need
If I find any file that start with error and have an extesion .out...then send an email(which is working fine without any conditions) and exit 1(stopped abruptly) else exit 0 (exit normally)
your help is much appreciated...Thank You
---------- Post updated at 03:42 PM ---------- Previous update was at 03:05 PM ----------
Corona,
your code worked...Thank you so much for the help.