开发者

get output of perl script from bash script and exit

开发者 https://www.devze.com 2023-02-26 18:31 出处:网络
From a bash script how can i execute a per开发者_如何学Gol script get the output and exit if value is = 0?

From a bash script how can i execute a per开发者_如何学Gol script get the output and exit if value is = 0? Also in the perl script how do i return the value, do i just return it or do i print it?


#!/bin/bash

perl-script args && exit

If you want to continue running, the return value is in $?

Note that there is a distinction between the value returned by the perl script and the output of the script. To get the output of the script, use the $() operator:

#!/bin/bash
output=$(perl-script args)

echo The perl script returned $?
echo The output of the script was $output


To get a return code, use the exit function. Example:

Perl script:

if ($success) {
    $return_value = 0;
} else {
    $return_value = 1;
}
exit($return_value);

Bash script:

perl scriptname args > outfile && exit

That's assuming that you want to exit if the return value of the Perl script is 0 and you want so save the output of the Perl script in outfile. If the return value is not zero, it's stored in $?, you can save that value to a variable if you please.


Your perl script: foo.pl

#!/usr/bin/perl
# do something and then
exit 1;

And inside your bash script foo.sh:

#!/bin/bash
# do something and then call perl script
perl foo.pl

# check return value using $?
echo "perl script returned $?"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号