如何检查系统命令是否成功

时间:2017-06-15 09:19:38

标签: perl

使用下面的代码如何在svn update中检查系统命令是否成功返回。

我希望我的脚本在svn update失败时失败,我该如何实现?

sub do_compile
{
 system("svn update");

 print "svn update successful\n";

 $ENV{'PATH'} = '/var/apps/buildtools/apache-maven-3.0.5/bin:/var/apps/java/jdk180_112_x64/bin';

$ENV{'JAVA_HOME'} = '/var/apps/java/jdk180_112_x64';

$ENV{'M2_HOME'} = '/var/apps/buildtools/apache-maven-3.0.5';

my $path = $ENV{'PATH'};

print "$path\n";

 system("mvn clean package -DskipTests=true -s /release/vgt/abc/.m2/artifactory_settings.xml");

2 个答案:

答案 0 :(得分:1)

System command returns zero if it is successful, and a non-zero status (with the appropriate error code) if it failed.

if (system("svn update")) {
    die "SVN update failed!";
}
else {
    print "SVN update successful";
}

Refer the following links for more information:

https://perldoc.perl.org/functions/system.html

Perl: After a successful system call, "or die" command still ends script

http://www.perlmonks.org/?node_id=343898

答案 1 :(得分:0)

跟随Jens',Sobrique和Naveen的领导。他们都很好。用简单的cmd测试...就像“ls”。确保测试为正(即ls *.txt)和否定(即ls *.huh)。然后替换真实的cmd(即svn ... )。

如果cmd表现良好,这应该有效。 cmd被认为是表现良好的,当它成功时会返回0,并且当它失败时会返回非零(也就是错误代码)。

如果cmd始终返回0(或者总是返回非零),请捕获输出并搜索关键字以确定成功/失败状态。例如,不要信任sqlplus cmd及其0退出代码。如果在输出中看到ORA- [0-9] +(Oracle环境),你可以放心地怀疑它在路上遇到了什么。