How can I execute a terminal command (like grep
) from my Objective-C Cocoa application?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
kent's article gave me a new idea. this runCommand method doesn't need a script file, just runs a command by a line:
You can use this method like this:
You can use
NSTask
. Here's an example that would run '/usr/bin/grep foo bar.txt
'.NSPipe
andNSFileHandle
are used to redirect the standard output of the task.For more detailed information on interacting with the operating system from within your Objective-C application, you can see this document on Apple's Development Center: Interacting with the Operating System.
Edit: Included fix for NSLog problem
If you are using NSTask to run a command-line utility via bash, then you need to include this magic line to keep NSLog working:
An explanation is here: https://web.archive.org/web/20141121094204/https://cocoadev.com/HowToPipeCommandsWithNSTask
If the Terminal command requires Administrator Privilege (aka
sudo
), useAuthorizationExecuteWithPrivileges
instead. The following will create a file named "com.stackoverflow.test" is the root directory "/System/Library/Caches".in the spirit of sharing... this is a method I use frequently to run shell scripts. you can add a script to your product bundle (in the copy phase of the build) and then have the script be read and run at runtime. note: this code looks for the script in the privateFrameworks sub-path. warning: this could be a security risk for deployed products, but for our in-house development it is an easy way to customize simple things (like which host to rsync to...) without re-compiling the application, but just editing the shell script in the bundle.
Edit: Included fix for NSLog problem
If you are using NSTask to run a command-line utility via bash, then you need to include this magic line to keep NSLog working:
In context:
An explanation is here: http://www.cocoadev.com/index.pl?NSTask
fork, exec, and wait should work, if you're not really looking for a Objective-C specific way.
fork
creates a copy of the currently running program,exec
replaces the currently running program with a new one, andwait
waits for the subprocess to exit. For example (without any error checking):There's also system, which runs the command as if you typed it from the shell's command line. It's simpler, but you have less control over the situation.
I'm assuming you're working on a Mac application, so the links are to Apple's documentation for these functions, but they're all
POSIX
, so you should be to use them on any POSIX-compliant system.