I am writing a wrapper to one of the .ksh script. The wrapper script should capture the error/fatal(if any) and exit out of the script. Here is the script:
#!/bin/sh
. main_script.ksh <arg1> <arg2> > mainscript.log 2>&1
cnt=`grep 'ERROR' -c mainscript.log`
if [ $cnt -ne 0 ]; then
echo "Error"
grep 'ERROR' mainscript.log > error.txt #Write the error to a file
exit 1
else
echo "Success"
fi
The above script is not doing what it is supposed to do. It is not erroring out and also not writing anything to the error.txt
When i take the execution of the main_Script.ksh from the script, it works fine but not when i am executing the main_script.ksh.
Nice you worked it out yourself, but it's still a little inefficient, your reading the whole file with grep to count the number of error lines and then re-reading it again to capture them.
Why not grep the error lines and redirect to error.txt and then test the size of error.txt with the -s option:
grep ERROR mainscript.log > error.txt
if [ -s error.txt ]; then
echo Error
...
else
rm error.txt
echo Success
fi