Recipe 20.9. Capturing the Output and Error Streams from a Unix Shell CommandProblemYou want to run an SolutionUse the open3 library in the Ruby standard library. Its popen3 method takes a code block, to which it passes three IO streams: one each for standard input, output, and error. Suppose you perform the Unix ls command to list a nonexistent directory. ls will rightly object to this and write an error message to its standard error stream. If you invoked ls with IO.popen or the %x{} construction, that error message is passed right along to the standard error stream of your Ruby process. You can't capture it or suppress it:
But if you use popen3, you can grab that error message and do whatever you want with it:
DiscussionThe same caveats in the previous recipe apply to the IO streams returned by Unlike IO.popen, the See Also
|
Friday, December 4, 2009
Recipe 20.9. Capturing the Output and Error Streams from a Unix Shell Command
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment