- 论坛徽章:
- 0
|
按照perldoc的描述,要获取一个命令调用的返回值有以下几种情况:
- @args = ( "command","arg1","arg2" );
- system(@args) == 0 dir "execute system command failed
复制代码
更加精确的捕捉:
-
- if ($? == -1) {
- print "failed to execute: $!\n";
- }
- elsif ($? & 127) {
- printf "child died with signal %d, %s coredump\n",
- ($? & 127), ($? & 128) ? 'with' : 'without';
- }
- else {
- printf "child exited with value %d\n", $? >> 8;
- }
复制代码
但是有时候为了图简便,我们有时候把所有命令里调用都放一条命令里,例如:
-
- system("tar cvf xxxx.tar;cd -") == 0 or die "can't tar file"
复制代码
这里就出现了一个bug,因为system总是返回最后一次调用的结果的,导致检测不成功,因此在调用
system执行多条命令如果需要捕捉返回结果,最好是分开执行. |
|