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

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 07:51:42

问题


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!


回答1:


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




回答2:


This worked perfectly for me.. it just closes that execution window leaving other terminal windows open

Just open Terminal and go to Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly

Then just add exit; at the end of your file.




回答3:


Use the 'Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly' option mentioned above, but put

exit 0

as the last line of your command file. That ensures the script really does 'exit cleanly' - otherwise if the previous command doesn't return success then the window won't close.




回答4:


There is a setting for this in the Terminal application. Unfortunately, it is relative to all Terminal windows, not only those launched via .command file.




回答5:


you could use some applescript hacking for this:

tell application "Terminal"
    repeat with i from 1 to number of windows
        if (number of (tabs of (item i of windows) whose tty is "/dev/ttys002")) is not 0 then
            close item i of windows
            exit repeat
        end if
    end repeat
end tell 

replacing /dev/ttys002 with your tty




回答6:


I'm using the following command in my script

quit -n terminal

Of course you have to have the terminal set to never prompt before closing.




回答7:


Short of having to use the AppleScript solutions above, this is the only shell script solution that worked (exit didn't), even if abruptly, for me (tested in OS X 10.9):

...
echo "$l" done
done

killall Terminal

Of course this will kill all running Terminal instances, so if you were working on a Terminal window before launching the script, it will be terminated as well. Luckily, relaunching Terminal gets you to a "Restored" state but, nevertheless, this must be considered only for edge cases and not as a clean solution.



来源:https://stackoverflow.com/questions/2565944/how-do-i-get-a-mac-command-file-to-automatically-quit-after-running-a-shell-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!