Error handling for sort command

Hello,

I need help in logging the error to the error log of one of sort command which i'm using and terminate the process if sort command is not able to execute.

sort -k 1.10,1.20n -o $outputfile $inputfile

how can i achieve the above.

Assuming you're using a POSIX conforming shell (such as ksh or bash):

if ! sort -k 1.10,1.20n -o $outputfile $inputfile 2>error_log
then    echo "terminating; sort failed"
        exit 1
fi

Hey, sorry for the late reply..

i tried with the above approach but it was giving me error something like [555] could not found ! ...
Anyways i went with the below approach and the process was terminating if sort is not working

sort -k 1.10,1.20n -o $inputfile $outputfile 2>>  error_log
pgmcc=$?
 
if [[ $pgmcc != 0 ]]
then
echo "FATAL ERROR, TERMINATING NOW"
fi

In this case i'm just getting the error written in error_log as sort: A write error occured while sorting.

But i want to capture error which comes on the console something like below:

vxfs: msgcnt 5980 mesg 001: V-2-1: vx_nospace - /dev/vx/dsk/drd_rootdg/tmpvol file system full (2 block extent)

Is there a way if i can achieve the above...??

No. The vxfs error message is not being written by sort; it is a diagnostic message being written to the console by the operating system. The 2>>errlog will append diagnostics written by sort to errlog, but can't capture messages written to the terminal by the operating system nor by unrelated processes.

Your system probably has a log file where error messages written to the console are saved (but if the filesystem where the log files would be written is full, the message might only appear on the screen). Where those log files are kept is system specific.

You may be using the & redirection

sort -k 1.10,1.20n -o $inputfile $outputfile &>>  error_log
if [ $? -ge 0 ];then echo "SORT ERROR";exit;fi

Regards

Sorry, did not see vxfs type of message, Don Cragun answer is the one related to your problem, not mine.

Sorry I didn't notice when I posted my last message ...

Note that you are asking sort to the read data from $outputfile and to write the sorted results to the file named by $inputfile. This seems backwards???

PS I'm afraid that the &>> redirection suggested by Ikaro0 won't help and won't do what I think he thinks it will do. The command:

sort ... &>>  error_log

will run sort in the background and create an empty file named error_log (if error_log didn't already exist). Output from sort will not go into error_log from this command, even if the sort -o option is not been specified.

Hi Don Cragun

As I corrected my self I did not see the vxfs type of message. I thought he wanted to redirect the error message that was already correct with "2>"
The "&>" does something that is redirecting both stdout and sderr the the file, but was a bad answer cause it did not help with the problem at all, sorry.