2
votes

I am trying to run a shell script in a MAC terminal. I want to open a new terminal window that will execute the script separate from the program that is running. In Fedora, there is a gnome-terminal command which lets me execute a script in another terminal shell.

Does anyone know an equivalent on MAX OSX and how to use it?

For example say I have a script crazy.sh and I want to call this from a program that is executing but in a separate terminal from the one which is currently executing the program.

2
Not so easy on the Mac. Do you want the results of the shell script? If so use popen(). If not use system(). - trojanfoe
nope, I don't care about the output of the script. Just need to run in. But system calls it within the same process and prints the output onto the same terminal window which is something I do not want. It floods the output - Edwin

2 Answers

4
votes

I like DigitalTrauma's answer but I found for my use, this worked better

open -a Terminal.app crazy.sh

Thanks for the answers.

2
votes

One way to do it is to use an xterm instead of a terminal window:

xterm -e crazy.sh

If you want the xterm to stay open after the script completes, use the -hold option to xterm.


But if you really need to do this in a terminal, you can do it with applescript:

tell application "Terminal"
    activate
    tell application "System Events" to keystroke "n" using command down
    repeat while contents of selected tab of window 1 starts with linefeed
        delay 0.1
    end repeat
    do script "crazy.sh" in window 1 -- make sure the path to your script is right
end tell

(Credit to the answer here https://superuser.com/questions/466619/open-new-terminal-tab-and-execute-script)