Jump to content

Running a Shell Script from Objective-C


5 posts in this topic

Recommended Posts

Hello:

 

I have been playing around with xcode a little bit recently, and have not been able to figure out how to run a bash shell script right from an implementation file. I looked into NSTask, but couldn't understand how to use it.

 

Is there a simple code block with a place to insert a shell script into it? If I could have some assistance with this it would be greatly appreciated. Thanks!

Link to comment
Share on other sites

Here's an example of running a shell script. The actual program being called, sh, is the program that runs the script. Assume the script was called sscript.sh and was in your resources project folder…

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/bin/sh"];
[task setArguments:[NSArray arrayWithObjects:[[NSBundle mainBundle] pathForResource:@"sscript" ofType:@"sh"], nil]];
[task launch];

 

setArguments: uses an NSArray to supply arguments to the task — in this case, the location of your script.

Link to comment
Share on other sites

  • 2 weeks later...
 Share

×
×
  • Create New...