lib/Shellex/Shellex.pm
1	package Shellex::Shellex;
2 use strict;
3 use warnings;
4 use Log::Log4perl qw(:easy);
5 use Exporter qw(import);
6 our @EXPORT_OK = qw(shellex findBin);
7
8 sub shellex {
9
10 my $cmd = shift;
11 my $logger = shift;
12 if ( defined $logger && $logger ne '' ) {
13 $logger->info("Running: $cmd 2>&1");
14 }
15
16 my $output = `$cmd 2>&1`;
17 chomp $output;
18 my $rc = $?;
19 if ( defined $logger && $logger ne '' ) {
20 $logger->info("Returned: $rc");
21 }
22
23 if ( $rc != 0 ) {
24 if ( defined $logger && $logger ne '' ) {
25 $logger->error("$cmd returned non-zero: $rc");
26 }
27
28 print "$cmd returned non-zero: $rc\n";
29 exit 1;
30 }
31
32 return $output;
33
34 }
35
36 sub findBin {
37
38 my $cmd = shift;
39 my $logger = shift;
40 my $binPath = shellex("which $cmd",$logger);
41 my $rc = $?;
42 chomp $binPath;
43 if ( $rc != 0 ) {
44 if ( defined $logger && $logger ne '' ) {
45 $logger->error("Couldn't find a path for $cmd, exiting...");
46 }
47 exit 1;
48 } else {
49 return $binPath;
50 }
51
52 }
53 1;