我正在尝试编写一个实用程序,它将通过一个看起来像这样的文件:
# 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
Run Code Online (Sandbox Code Playgroud)
我的脚本将通过文件决定是否,例如,/ work/test /下的文件以'a'开头,在过去1天内没有被修改过,如果有,它会删除它们.
为此,我使用find命令.例:
my $command = "find " . $values[0] . $values[1] . " -mtime +" . $values[3] . " -delete ;\n";
system ($command);
Run Code Online (Sandbox Code Playgroud)
但是,我被要求检索每个步骤的返回码,以验证每个步骤是否正常工作.
现在,我知道system()返回返回代码,反引号返回输出.但是,我怎样才能得到两者?
mob*_*mob 13
运行反引号后,返回代码可用$?.
$?最后一个管道关闭,反引号(``)命令,成功调用wait()或waitpid()或来自system()操作符返回的状态.这只是传统Unix wait()系统调用返回的16位状态字(或者看起来像是这样).
$output = `$some_command`;
print "Output of $some_command was '$output'.\n";
print "Exit code of $some_command was $?\n";
Run Code Online (Sandbox Code Playgroud)