I have a bash script having the following command
rm ${thefile}
In order to ensure the command is execute successfully, I use $? variable to check on the status, but this variable doesn't show the exact error? To do this, I redirect the standard error output to a log file using following command:
rm ${file} >> ${LOG_FILE} 2>&1
With this command I can't use $? variable to check status on the rm command because the command behind the rm command is executed successfully, thus $? variable is kind of useless here.
May I know is there a solution that could combine both features where I'm able to check on the status of rm command and at mean time I'm allow to redirect the output?
$?variable to check status on thermcommand' is incorrect. The exit status of the command is independent of where the I/O redirection sends standard output and standard error (except that if the I/O redirections fail, the command is not executed at all). Given the false premise, it is not surprising that your conclusion 'thus $? variable is kind of useless here' is incorrect. You can useif rm ${file} >> ${LOG_FILE} 2>&1; then : OK; else : Failed; fias just one of many ways of testing the status of thermcommand.