mahesh
mahesh

Reputation: 41

How to check if system command is successful or not

Using below code how do I check if system command is returning successful or not in svn update.

I want my script to fail when svn update failes, how can I achieve this?

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");

Upvotes: 1

Views: 6648

Answers (2)

ky bof
ky bof

Reputation: 89

Follow Jens', Sobrique's and Naveen's leads. They're all good. Test out with simple cmd ... like "ls". Make sure to test for positive (ie. ls *.txt) and negative (ie. ls *.huh). Then substitute the real cmd (ie. svn ... ).

This should work IF the cmd is well-behaved. A cmd is considered well-behaved would return 0 when it succeeds and would return non-zero (aka error code) when it fails.

In case a cmd always returns 0 (or always returns non-zero for that matter), capture the output and search for keyword(s) to determine the success/fail status. For example, dont trust sqlplus cmd with its 0 exit code. If seeing ORA-[0-9]+ (Oracle environment) in the output, you can safely suspect that it fumbles on something along the way.

Upvotes: 0

Naveen P
Naveen P

Reputation: 77

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

Upvotes: 3

Related Questions