简体   繁体   中英

NSTask sending an argument through /bin/sh or /bin/bash problem Cocoa Objective-C

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!

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

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.

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]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM