views:

47

answers:

2

Hello,

Basically I've passed a script in the terminal through /bin/bash "[path]" or /bin/sh "[path]", and I've passed commands in a terminal through /bin/bash -c "[command]" or /bin/sh -c "[command]". Both ways work properly. But in my Cocoa App, when I try to do the exact same thing with NSTask (using /bin/bash or /bin/sh), the app seems to never respond. It is as if the program is stuck inside of bash or sh. I've tried to do this with and without waitUntilExit, and I've also tried to use terminate. Still no luck. Has anyone else had this issue or have a clue as to why this is happening?

Thanks!

P.S. I'm not on my work computer right now, but, if needed, I can provide code later.

A: 

Ok. So I found the solution for anyone else that might need it. Basically, there is a problem is the standardInput. The problem only shows up in the Xcode console and is not an error/bug with your app. The fix is to add the following line (basically set the standardInput to something random):

[task setStandardInput: [NSPipe pipe]];

In the solution above, task is the variable name of the NSTask being used.

hassaanm
A: 

Also see:

http://www.cocoadev.com/index.pl?HowToPipeCommandsWithNSTask

[proc setLaunchPath:@"/bin/bash"];
[proc setArguments:[NSArray arrayWithObjects: @"-c", @"/usr/sbin/netstat -rn | /usr/bin/grep default", nil]];
joe