I have an application that I'm creating and would like to run a shell scripts from within it. One of the scripts creates a .plist file, and the other does a patch on a binary file. The reason why want to do this with a shell script is because I know that it works for the two things that I need it for. I'm sure there's a way to do it with Objective-C/Cocoa although I don't know how, so any recommendations on how to accomplish this would be greatly appreciated
Asked
Active
Viewed 9,142 times
3
-
You mean MacOS or? You can forget that with iOS, tho you can easily create plists and patch binaries directly from obj-c (in the writable directory only). – ott-- Jan 02 '12 at 21:26
-
Sorry I didn't clarify, it's for OS X. – Jan 02 '12 at 21:33
-
possible duplicate of [Execute a terminal command from a Cocoa app](http://stackoverflow.com/questions/412562/execute-a-terminal-command-from-a-cocoa-app) – mmmmmm Sep 14 '14 at 21:32
2 Answers
5
You'll need to use NSTask
. NSTask
is a system for running any Terminal command. Here's how you could use it to execute a shell script:
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/path/to/script/sh"];
[task setArguments:[NSArray arrayWithObjects:@"yourScript.sh", nil]];
[task setStandardOutput:[NSPipe pipe]];
[task setStandardInput:[NSPipe pipe]];
[task launch];
[task release];
That would run the command /path/to/script/sh yourScript.sh
. If you need any arguments for your script, you can add them to the array of arguments. The standard output will redirect all output to a pipe object, which you can hook into if you want to output to a file. The reason we need to use another NSPipe
for input is to make NSLog
work right instead of logging to the script.
If you want more info on how NSTask
works, see this answer.

Community
- 1
- 1

Alexis King
- 43,109
- 15
- 131
- 205
-
Thank you for the awesome example. Is there a way to compile the script into the binary? – Jan 02 '12 at 21:39
-
@Buxme: You can add the shell script to your project and get the path to it with `[[NSBundle mainBundle] pathForResource:@"scriptName" ofType:@"sh"]`. Then just use that path for the first argument, and it should run just fine. – Alexis King Jan 03 '12 at 01:00
-
Will this cause the terminal to open to execute the script, or will this cause the application in objective-c itself to run the script without opening the terminal? – John Militer Feb 26 '18 at 22:48
3
The easiest way I know is NSTask. It's just like a terminal command from where you can call your bash script.

Peter Hosey
- 95,783
- 15
- 211
- 370

Gary
- 83
- 4
-
Even better than a terminal command, since you pass the arguments in an array and your program runs the task directly; with no shell involved, you don't need to worry about shell escaping. – Peter Hosey Jan 02 '12 at 21:31
-
Cool. Do you have by chance have an example? Also, will it not be obvious that my application is calling a shell script, or is this not really even a shell script? BTW - I would want the script to be compiled into the binary. – Jan 02 '12 at 21:32
-
@Buxme: What do you mean by “obvious”? The user can't tell that you're running another process unless they look in Activity Monitor. – Peter Hosey Jan 03 '12 at 03:26
-