pgduke65 has asked for the wisdom of the Perl Monks concerning the following question:
Monks,
I have a situation in which I want to search command output for lines containing any of a specified list of strings. I have scoured the net, Perl docs, and written the code below that is still not working. Can someone please help me understand where I am going wrong?
use constant { TRUE => 1, FALSE => 0 }; our $SUCCESS = 0; our %ErrorStrings = ( "No such file or directory" => TRUE, "does not exist in file" => TRUE, ); # Run command. @Output = qx { command here 2>&1 }; $RetCode1=$?; # If the return was success, we need to scan the output for errors. if ($RetCode1 == $SUCCESS) { # Scan the output and build a list of errors. my @FileErrors = grep { exists $ErrorStrings{$_} } @Output; if (@FileErrors) { $RetCode1 = scalar(@FileErrors); printf "Errors found: [%d] ", $RetCode1; push (@ProcErrors, @FileErrors); } # Non success error code. } else { printf "Error Code: [%d] ", $RetCode1; } # Output a status. printf "%-15s\n", ($RetCode1 == 0) ? "SUCCESS":"FAILED";
Which produces:
Processing A_Ra -> A/R ... SUCCESS Processing ADDRESS -> ADDRESS ... SUCCESS Processing ADJ-HIST -> ADJ-HIST ... SUCCESS Processing ALT-PN -> ALT-PN ... SUCCESS Processing AR-LEDGER -> AR-LEDGER ... SUCCESS Processing AR_LEVEL -> AR/LEVEL ... SUCCESS Processing BCBI -> BCBI ... SUCCESS Processing BID -> BID ... SUCCESS Processing BUDGET -> BUDGET ... SUCCESS Processing BUDGET_HEADER -> BUDGET/HEADER ... SUCCESS
And I have intentionally introduced an error into the command being called. What's more, the output of the command with the error is below:
'XORDER' does not exist in file. Open file error.
Please help me understand what I am doing wrong.
Thank you
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Confusion with finding strings in command output
by Laurent_R (Canon) on Aug 30, 2014 at 17:53 UTC | |
by pgduke65 (Acolyte) on Aug 30, 2014 at 19:18 UTC | |
|
Re: Confusion with finding strings in command output
by Anonymous Monk on Aug 30, 2014 at 18:02 UTC | |
by Laurent_R (Canon) on Aug 30, 2014 at 18:55 UTC | |
by Anonymous Monk on Aug 30, 2014 at 20:07 UTC | |
by Laurent_R (Canon) on Aug 30, 2014 at 20:37 UTC |