macos 运行shell脚本后如何让Mac“.command”文件自动退出?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2565944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:50:48  来源:igfitidea点击:

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

macosshellscriptingcommand

提问by LOlliffe

In my shell script, my last lines are:

在我的 shell 脚本中,我的最后几行是:

...
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:

我将终端首选项设置为“当 shell 退出时:关闭窗口”。在所有其他情况下,当我在终端中键入“exit”或“logout”时,窗口会关闭,但对于这个“.command”文件(我可以双击我的 shell 脚本文件,然后脚本运行),而不是关闭窗口,而文件的代码说“退出”,屏幕上显示的是:

...
$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?

...而窗户保持打开状态。有谁知道如何让 shell 脚本运行,然后在完成后自动退出终端窗口?

Thanks!

谢谢!

回答by LOlliffe

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.

我终于能够找到这个问题的答案。与 cobbal 的回答类似,它调用 AppleScript,但由于它是我唯一打开的窗口,而且我想将脚本作为快速打开和关闭操作运行,这种更粗暴的方法对我来说非常有用。

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

在“.command”脚本本身中,“...最后将此行添加到您的脚本中”

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

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

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

回答by Sagar Patil

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.

然后只需添加退出; 在您的文件末尾。

回答by pooroldpedro

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.

作为命令文件的最后一行。这确保脚本确实“干净地退出” - 否则,如果前一个命令没有返回成功,则窗口将不会关闭。

回答by pooroldpedro

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

不必使用上面的 AppleScript 解决方案,这是唯一一个有效(exit没有)的shell 脚本解决方案,即使是突然的,对我来说也是如此(在 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.

当然,这会杀死所有正在运行的终端实例,因此如果您在启动脚本之前在终端窗口上工作,它也会被终止。幸运的是,重新启动终端会使您进入“已恢复”状态,但是,这必须仅在边缘情况下才考虑,而不是作为干净的解决方案。

回答by mouviciel

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.

在终端应用程序中有一个设置。不幸的是,它与所有终端窗口有关,而不仅仅是通过 .command 文件启动的那些窗口。

回答by cobbal

you could use some applescript hacking for this:

您可以为此使用一些applescript hacking:

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/ttys002with your tty

/dev/ttys002你的 tty替换

回答by cobbal

I'm using the following command in my script

我在我的脚本中使用以下命令

quit -n terminal

退出 -n 终端

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

当然,您必须将终端设置为在关闭前从不提示。