I have a PERL command line embedded in a UNIX script. The script doesn't handle errors coming out of this command. I'm processing large files and occassionally I run out of disk space and end up with half a file.
perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4
How do I handle errors coming out of this type of command?
Well you should consider dos2unix (may br dos2ux on your box) to clean up windows carriage returns. It was meant to do that. And has error handling.
Otherwise, you have to do error handling inside the perl script itself.
You can add external checks:
# note: this is kind of generalized for basic file I/O checking, and is overkill for this particular perl one liner.
if [ -r TR_TMP_$4 ] ; then
touch $4 || echo "cannot write $4" && exit
perl -p -e 's/\n/\r\n/g' < TR_TMP_$4 > $4
if [ $? -ne 0 ] ; then
echo "fatal error "
exit
fi
else
echo "cannot read TR_TMP_$4"
exit
fi
Note: consider forming TR_TMP_$4 as TR_TMP_${4} It is two more keystrokes but if you do it consistently it will save ambiguity in reading code, and has helped me catch mistakes lots faster. YMMV.