system function output

33 views
Skip to first unread message

Rod Schmidt

unread,
Feb 16, 2013, 4:07:46 PM2/16/13
to program...@googlegroups.com
How do I capture the output of the system function? Or do I have to do it the Obj-C way and create a pipe and capture it that way?

Jeff Buck

unread,
Feb 16, 2013, 5:49:05 PM2/16/13
to program...@googlegroups.com
I don't think there's a built-in way to capture the output, but here are two quick and dirty functions that will do it for you. One uses a pipe. The other one uses a redirection to a temp file and redirects stderr to stdout in case you want to capture error messages.


-Jeff

Andrew Schleifer

unread,
Feb 16, 2013, 6:03:11 PM2/16/13
to program...@googlegroups.com


On Feb 16, 2013, at 3:07 PM, Rod Schmidt <rod.s...@gmail.com> wrote:
> How do I capture the output of the system function? Or do I have to do it the Obj-C way and create a pipe and capture it that way?


If you use "system", then you'll have to do it manually. However, there's an extension on NSString that provides a "stringWithShellCommand:" class method, that might do what you want:

$ nush
Nu Shell.
% (NSString stringWithShellCommand:"ls ~")
"Applications\nBackups\nDesktop\nDocuments\nDownloads\nDropbox\nLibrary\nMovies\nMusic\nPictures\nPublic\nSites\nSource\nWork"

Reply all
Reply to author
Forward
0 new messages