Note that in a list context (e.g. assigning to an array), the backticks will split on line breaks for you, and retain the line-feed at the end of each line (each element of the array).my @size = `du -sk /tmp/dir* | cut -f1`;
If the "du" fails there, @size will be empty, but the error report from du will go to your STDERR. If you want to catch the error report, and you are using a bourne-style shell (as opposed to a csh-style shell), you can redirect the shell's stderr, as explained at length in the description of "qx" (backtick opertator) in "perldoc perlop":
This will put the error message as the sole element of @size. Then again, if you somehow get warnings on some files/paths, like "permission denied", these will be mixed in with size numbers for paths that had no problems -- which makes @size kind of a mess... So you could redirect the shell's stderr to a file (if you want to read it later from the perl script) or just redirect to /dev/null (if you don't need to read it).my @size = `du -sk /tmp/dir* 2>&1 | cut -f1`;
In reply to Re: Capturing shell command error
by graff
in thread Capturing shell command error
by kulls
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |