Bulrush
Bulrush

Reputation: 558

How to check status of error code in csh?

This is not a Perl questions, this is a csh question.

Perl 5.8.8 on Linux RHEL 5.5.56.

I have a test Perl program which uses 'exit 3;' to set the exit status. The Perl program is run from a csh script I call 'test'.

Perl program:

#/usr/bin/perl
# July 11, 2014
# Test exit code with csh $status variable
my($i);
$i=3;
print "$0 Exit with error code $i\n";
exit $i;

'test' csh script that calls Perl progam:

#/bin/csh
# July 10, 2014,

perl tstatus.pl
echo "Status=$status"
if ($status > 0) then
    echo "ERROR in status: $status"
endif

The line 'echo "ERROR in status: $status" is never executed even though 'echo "Status=$status' says status is 3.

How do I get my csh to check and act on the value of $status? $status is a system variable it seems.

I have also tried 'if ($stats) then' but that doesn't work either. No errors are reported in the csh script.

Thank you.

p.s. I don't do complicated stuff in csh so I'm not ready to go to bash yet. I've read the "why not csh" file already. Thanks.

Upvotes: 2

Views: 14992

Answers (2)

user14759279
user14759279

Reputation: 11

every line executed in csh sets $status

I usually follow any statement, where I want to test the status with something like:

set myrc = $status

which will persist regardless of the number of statements that are executed in between call and the evaluation of the result.

eg:

    perl tstatus.pl
    set myrc = $status
    echo "Status=$myrc"
    if ($myrc> 0) then
        echo "ERROR in status: $myrc"
    endif

Upvotes: 1

John McGehee
John McGehee

Reputation: 10339

The echo command succeeds, resetting $status to 0. Thus if never sees nonzero $status.

Therefore the solution is to either eliminate the echo command or save the value of $status immediately after the perl command.

I recently encountered a similar issue myself.

Upvotes: 3

Related Questions