Operating System - Linux
1821187 Members
3200 Online
109631 Solutions
New Discussion юеВ

Re: pass the return status of perl script to shell script

 
chinnaga
Advisor

pass the return status of perl script to shell script

Hi,

I am calling a perl script to ftp files from one server to another server and I want the exit status of the perl script to be passed to the calling shell script so tht I can send success or failure mail from shell script
7 REPLIES 7
Tim Nelson
Honored Contributor

Re: pass the return status of perl script to shell script

Not a perl expert but typically the end of a script would be completed by exit 1 (or some non zero )for a failure. You can then use the exit code with $? for error detection in the parent.

James R. Ferguson
Acclaimed Contributor

Re: pass the return status of perl script to shell script

Hi:

As with any process, exit with the value you wish to propagate to the caller.

In this case, your Perl script "knows" the status of the FTP action. For example:

...
$ftp->login() or die "Can't login!";
...

In this case, the 'die' causes the Perl script to exit with a non-zero return status.

If you want to handle other conditions and exit later, set a variable with some return code and simple 'exit($lastaction)'.

In any case, your shell wrapper can then test the '$?' variable for the result of the Perl script just like any command's return status.

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: pass the return status of perl script to shell script

Hi,

besides checking the variable $?, you can directly use something like the following in your shell script, when no further handling of the exit-status is required:

#!/usr/bin/sh
...
if my-perl-script.pl arg1 arg1;
then # further processing
print OK
else
print -u2 ERROR in xxx
exit 1
fi
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor

Re: pass the return status of perl script to shell script

Hi,
small additions:
- you could drop the ';' at the end of the 'if' condition
- you could pass $? to the calling environment of the shell script

#!/usr/bin/sh
...
if my-perl-script.pl arg1 arg1
then # further processing
print OK
else
print -u2 ERROR in xxx
exit $?
fi
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: pass the return status of perl script to shell script

>Peter: you could drop the ';' at the end of the 'if' condition

Better yet, keep it and do:
if ... ; then

>you could pass $? to the calling environment of the shell script

I'm not sure this will work. The print will destroy $?.

my-perl-script.pl arg1 arg1
result=$?
if [ $result -eq 0]; then # further processing
print OK
else
print -u2 ERROR in xxx
exit $result
fi
chinnaga
Advisor

Re: pass the return status of perl script to shell script

Thanks a lot , it worked.
Peter Nikitka
Honored Contributor

Re: pass the return status of perl script to shell script

Hi,

Dennis is right - either print or pass the exit status:

EITHET THIS:
#!/usr/bin/sh
...
if my-perl-script.pl arg1 arg1
then # further processing
print OK
else
print -u2 ERROR $? in xxx
exit 1
fi
...

OR THIS:
#!/usr/bin/sh
...
if my-perl-script.pl arg1 arg1
then # further processing
print OK
else
exit $?
fi
...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"