I want to capture the output of several commands into a logfile. In between these commands, a value is assigned to a variable.
Example code 1:
----------
#!/bin/ksh
{
command1
command2
important_command3
exit_code=${?}
command4
} |tee ${LOGFILE}
if [ ${exit_code} -ne 0 ]
then mailx -s"Failure" ${RECIPIENT} <${LOGFILE}
fi
----------
When running the script the 'if' statement fails because ${exit_code} does not exist. However, when running the following example, the variable ${exit_code} is suddenly known and the 'if' statement does not present any errors:
Example code 2:
----------
#!/bin/ksh
{
command1
command2
important_command3
exit_code=${?}
command4
}
if [ ${exit_code} -ne 0 ]
then mailx -s"Failure" ${RECIPIENT} <${LOGFILE}
fi
----------
Does anyone have an idea why the variable get lost when 'tee-ing' the output into the logfile?
Example code 1:
----------
#!/bin/ksh
{
command1
command2
important_command3
exit_code=${?}
command4
} |tee ${LOGFILE}
if [ ${exit_code} -ne 0 ]
then mailx -s"Failure" ${RECIPIENT} <${LOGFILE}
fi
----------
When running the script the 'if' statement fails because ${exit_code} does not exist. However, when running the following example, the variable ${exit_code} is suddenly known and the 'if' statement does not present any errors:
Example code 2:
----------
#!/bin/ksh
{
command1
command2
important_command3
exit_code=${?}
command4
}
if [ ${exit_code} -ne 0 ]
then mailx -s"Failure" ${RECIPIENT} <${LOGFILE}
fi
----------
Does anyone have an idea why the variable get lost when 'tee-ing' the output into the logfile?