微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Perl中的命令获取返回代码和输出

我正在尝试编写一个实用程序,它将通过一个看起来像这样的文件
# Directory | file name | action | # of days without modification to the file for the command to take action
/work/test/|a*|delete|1
/work/test/|b*|compress|0
/work/test/|c*|compress|1

我的脚本将通过文件决定是否,例如,/ work / test /下的文件以’a’开头,在过去1天内没有被修改过,如果有,它会删除它们.

为此,我使用find命令.例:

my $command = "find " . $values[0] . $values[1] . " -mtime +" . $values[3] . " -delete ;\n";
system ($command);

但是,我被要求检索每个步骤的返回码,以验证每个步骤是否正常.

现在,我知道system()返回返回代码,反引号返回输出.但是,我怎样才能得到两者?

解决方法

运行反引号后,返回代码$?中可用.

$?

The status returned by the last pipe close,backtick (“) command,successful call to wait() or waitpid(),or from the system() operator. This is just the 16-bit status word returned by the Traditional Unix wait() system call (or else is made up to look like it).

$output = `$some_command`;
print "Output of $some_command was '$output'.\n";
print "Exit code of $some_command was $?\n";

原文地址:https://www.jb51.cc/Perl/171977.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐