0

I wrote a Perl script, and it returned 1 or 0 depending on whether it fails/succeeds. I had a .csh shell script that reads the return value.

The .csh command is this:

> setenv CHECKER `perl $BIN_DIR/sgRevisionChecker.pl`
if($CHECKER) then
    do stuff
else
    echo 'Successful Run'
exit
endif

However, the Perl script won't pass the correct value even when in the Perl script I say "exit 1" or "exit 0".

However, if within the Perl script I do this:

print "1";
exit 1;

Then my shell script gets the value. It seems to get the value if I print it, but I don't think this is robust, and I want to do it the right way. I have tried other solutions, but printing seems to be the only fix.

What is the correct way to return a value from Perl to a .csh script?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Well, in bash, `RETURN=$( some_script.pl )` would set $RETURN to whatever the script printed. The proper thing to do would be `some_script.pl; RETURN=$?` – kjprice Jun 13 '13 at 23:23
  • A quick google: http://bima.astro.umd.edu/checker/node22.html You want the $status variable. – kjprice Jun 13 '13 at 23:24
  • I am not using bash. That doesn't work. I am in tcsh I believe. – Clifford Maxwell Jun 13 '13 at 23:25
  • Type this in: "ps -p $$" (without quotes) to get the shell that you are using – jh314 Jun 13 '13 at 23:28
  • @CliffordMaxwell Yes, I was just trying to give you an idea with my original comment. I assumed that it was similar in csh. – kjprice Jun 13 '13 at 23:28

1 Answers1

0
$BIN_DIR/sgRevisionChecker.pl
if($status) then
    # do stuff
else
    echo 'Successful Run'
    exit
endif

I just tried this with:

$ cat test.sh
#!/bin/bash 

exit 1

$ cat c.sh 
#!/bin/tcsh

./test.sh
if($status) then
    echo 'Fail'
else
    echo 'Successful Run'
endif

$ ./c.sh
Fail

Or even shorter :

> perl -e 'exit 1'
> echo $status
1
kjprice
  • 1,316
  • 10
  • 26