PDA

View Full Version : Invoking a single command in Terminal



chrism238
24th July 2007, 09:34 AM
When using an xterm on most Unix systems (including OSX) my project's needs are met by invoking xterm -e myprogram its_arguments - a new X terminal window opens, myprogram is invoked "within" it, and the new xterm manages the ASCII I/O. I can further use xterm's -hold command-line option to keep the window open after myprogram terminates. This all works well under OSX, but it does require the X11 server to be running.

Is there any equivalent for Apple's native Terminal program? Can I invoke Terminal and provide a "one-off" command to execute within it?

And, if so, how did you find such information - without a standard online manual for Terminal, Terminal appearing to accept no command-line options, and Apple's Help on Terminal providing little, I'm lost! Thanks,

purana
24th July 2007, 09:41 AM
I am guessing you could use an AppleScript procedure to open terminal, execute a command.. and keep it open? Although I do not know AppleScript myself, so cannot provide an example.

chrism238
24th July 2007, 09:53 AM
I am guessing you could use an AppleScript procedure to open terminal, execute a command.. and keep it open? Although I do not know AppleScript myself, so cannot provide an example.Thanks, but all of this happens from within a C program - currently the xterm is invoked using execve(....)

If AppleScript offers some assistance (I don't wish to use it because I may as well just keep running the X11 server!) I'd like to learn how AppleScript does it, and then I can mimic that from my C code.

purana
24th July 2007, 09:57 AM
Thanks, but all of this happens from within a C program - currently the xterm is invoked using execve(....)

Oh really, guess you never mentioned that. In that case I think your stuck using an xterm :)

dotnet
24th July 2007, 11:02 AM
From the command line you can invoke Terminal with an argument being a text file containg shell comands. Hence, if you put


/bin/ls -la

into a file called command you can run it in a Terminal window like this:


$ /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal ~/command

(for some reason Terminal wants an absolute path for the command file).

You should be able to do the same from within C.

Cheers
Steffen.

chrism238
24th July 2007, 11:28 AM
From the command line you can invoke Terminal with an argument being a text file containg shell comands. Hence, if you put

/bin/ls -la

into a file called command you can run it in a Terminal window like this:

$ /Applications/Utilities/Terminal.app/Contents/MacOS/Terminal ~/command

(for some reason Terminal wants an absolute path for the command file).

You should be able to do the same from within C.
Thanks Steffen, yes, I had tried these basics but try anything "complex" like appending /bin/ls -l or "/bin/ls -l" or "/bin/sh -c '/bin/ls -l'" and watch Terminal randomly fail to launch, get stuck in the Dock, or cause a need for a reboot (don't try some of these at home!)
Basic Unix stuff, but far from as slick as most other Apple software.
Gotta go, time to reboot again.....