Petro
Petro

Reputation: 3652

$? in shell script

I'm trying to figure out what this means/how $? gets populated in linux, I tried doing a search but if someone could clarify that would be great:

exitstat=$?

if [ $exitstat -ne 0 ]
  then
    echo -e "Could Not Extract"
    echo -e "Aborting Script `date`"
    exit $exitstat
fi

The code above that is:

_xfile << %%  2> /files/thefile-7000.log |  _afile -x -r 10 2> /files/thefile-7000.log > /files/thefile.7000
OperatorHigh = $finalnumber
%%

Upvotes: 1

Views: 541

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295619

$? expands to the exit status of the most recent foreground command.

Since your prior command is a pipeline, the exit status is that of the last command in the pipeline -- in this case, _afile -- unless the pipefail shell option is set, in which case failures elsewhere in the pipeline can also make exit status nonzero.

Upvotes: 4

Related Questions