LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Check previous error status (http://www.linuxquestions.org/questions/programming-9/check-previous-error-status-620278/)

thunder44 02-11-2008 07:02 AM

Check previous error status
 
Hello,

This statement works if there is an error within the last command in
Line 01. I have to modify the code in Line 02 so that it checks for
errors in previous statements in Line 01. I want to get the error status
for previous commands in the statement in Line 01 such as the error
status for the command 'cat $ORA_LIST_FILE'. How to do it?

Statements:
01 cat $ORA_LIST_FILE | wc -l 1> $ORA_LIST_COUNT_FILE_TMP 2>>
${ORA_LOG_FILE}
02 if [ ! $? -eq 0 ]; then
03 exit
04 fi

cmnorton 02-11-2008 08:25 AM

$?
 
Even if you capture the output in an environment variable, you should be able to find the status in $?.

IMPOSSIBLE_TO_FIND_FILE=`ls this_file_is_not_here`

if [ $? -eq 0 ]; then
echo "Impossible file found."
else
echo "Impossible file not found."
fi

Matir 02-11-2008 09:02 AM

The only return value available is that of the last command in a pipeline.

Hobbletoe 02-11-2008 10:07 AM

Your problem should go away if you skip catting the file out. Just use wc.

Code:

wc -l ${ORA_LIST_FILE} 1> $ORA_LIST_COUNT_FILE_TMP 2>>${ORA_LOG_FILE}

chrism01 02-13-2008 06:32 PM

Indeed: 'useless use of cat', also, don't use
[[ ! $? -eq 0 ]]
use
[[ $? -ne 0 ]]

PS Matir is correct also


All times are GMT -5. The time now is 11:10 PM.