Shellex.pm 948 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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. sub shellex {
  8. my $cmd = shift;
  9. my $logger = shift;
  10. if ( defined $logger && $logger ne '' ) {
  11. $logger->info("Running: $cmd 2>&1");
  12. }
  13. my $output = `$cmd 2>&1`;
  14. chomp $output;
  15. my $rc = $?;
  16. if ( defined $logger && $logger ne '' ) {
  17. $logger->info("Returned: $rc");
  18. }
  19. if ( $rc != 0 ) {
  20. if ( defined $logger && $logger ne '' ) {
  21. $logger->error("$cmd returned non-zero: $rc");
  22. }
  23. print "$cmd returned non-zero: $rc\n";
  24. exit 1;
  25. }
  26. return $output;
  27. }
  28. sub findBin {
  29. my $cmd = shift;
  30. my $logger = shift;
  31. my $binPath = shellex("which $cmd",$logger);
  32. my $rc = $?;
  33. chomp $binPath;
  34. if ( $rc != 0 ) {
  35. if ( defined $logger && $logger ne '' ) {
  36. $logger->error("Couldn't find a path for $cmd, exiting...");
  37. }
  38. exit 1;
  39. } else {
  40. return $binPath;
  41. }
  42. }
  43. 1;