indexing - Perl Index function not working? -


i'm trying test if backtick output string (it string, right??) contains substring.

my $failedcoutner = 0; $taroutput = `tar -tvzf $tgzfile`; print "$taroutput\n";  $substr = "cannot open: no such file or directory"; if (index($taroutput, $substr) != -1) {     push(@failedfiles, $tgzfile);     $failedcounter++;     print "number of failed files: $failedcounter\n\n\n"; } print "number of failed files: $failedcounter\n\n\n"; 

but isn't working. never enters if statement.

the backtick output:

tar (child): /backup/arcsight/edssim004: cannot open: no such file or directory tar (child): error not recoverable: exiting tar: child returned status 2 tar: error not recoverable: exiting  number of failed files: 0 

clearly substring in first line. why won't recognize this??

tar, programs, writes error messages stderr. that's purpose of stderr.

backticks capture stdout.

you redirect tar's stderr stdout, why not check exit code.

system('tar', '-tvzf', $tgzfile); die "can't launch tar: $!\n" if $? == -1; die "tar killed signal ".($? & 0x7f) if $? & 0x7f; die "tar exited error ".($? >> 8) if $? >> 8; 

advantages:

  • catches errors, not one.
  • the output isn't held until tar finishes before being sent screen.
  • it solves problem of archives shell metacharacters (e.g. spaces) in name without invoking string::shellquote's shell_quote.

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -