Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get a Mac ".command" file to automatically quit after running a shell script?

In my shell script, my last lines are:

... echo "$l" done done  exit 

I have Terminal preference set to "When the shell exits: Close the window". In all other cases, when I type "exit" or "logout", in Terminal, the window closes, but for this ".command" file (I can double-click on my shell script file, and the script runs), instead of closing the window, while the file's code says "exit", what shows on the screen is:

... $l done logout  [Process completed] 

...and the window remains open. Does anyone know how to get a shell script to run, and then just automatically quit the Terminal window on completion?

Thanks!

like image 666
LOlliffe Avatar asked Apr 02 '10 09:04

LOlliffe


People also ask

How do I close a .sh file on Mac?

As far as stopping a shell script running in the background, just reboot, or if you started that shell script, logout, and sign back in to your Mac. That will kill all applications that you started in the previous session. Everything VikingOSX said. sh is /bin/sh and is the Unix Bourne Shell.

How do you stop a Mac program from running in Terminal?

Terminate commands In the Terminal app on your Mac, click the Terminal window that is running the command you want to terminate. Press Control-C. This sends a signal that causes most commands to terminate.

How do I stop scripts on Mac?

Command + period (.) You can also kill (force quit) Script Editor and, provided it autosaves your documents, the scripts will be restored to the last compiled versions when you reopen.

How do I run a .command file in Terminal Mac?

To enable execute permissions, open Terminal and type chmod 755 /path/to/script . Instead of typing the full path, you can drag the script onto the Terminal window from Finder. Then, to execute, just enter /path/to/script . Again, you can drag and drop the file onto the Terminal window.


1 Answers

I was finally able to track down an answer to this. Similar to cobbal's answer, it invokes AppleScript, but since it's the only window that I'd have open, and I want to run my script as a quick open-and-close operation, this more brutish approach, works great for me.

Within the ".command" script itself, "...add this line to your script at the end"

osascript -e 'tell application "Terminal" to quit' & exit 

SOURCE: http://forums.macosxhints.com/archive/index.php/t-2538.html

like image 127
LOlliffe Avatar answered Oct 11 '22 21:10

LOlliffe