macos 从 shell 脚本 (Unix) 中关闭终端窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8798641/
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 08:45:23  来源:igfitidea点击:

Close Terminal window from within shell script (Unix)?

macosshellunixcommand

提问by ericsoco

Is there a way to close a Terminal window from within a shell script? I have a .command file that should just get out of the way once it's done.

有没有办法从 shell 脚本中关闭终端窗口?我有一个 .command 文件,一旦它完成就应该让开。

回答by Simon Urbanek

Using exit 0will cleanly terminate the script.

使用exit 0将干净地终止脚本。

Whether Terminal window stays open is user-configurable. The default is to always stay open. To change this:

终端窗口是否保持打开是用户可配置的。默认设置是始终保持打开状态。要改变这一点:

Terminal.app > Preferences > Profiles > Shell
    - "When the shell exists:"
        > Close if the shell exited cleanly
    - "Ask before closing:"
        (?) Never
        -- OR --
        (?) Only if there are....

When "Close if shell exited cleanly" is used, the script will close the window if the exit result is 0, which is the default if nothing went wrong.

当使用“Close if shell exited clean”时,如果退出结果为 0,脚本将关闭窗口,如果没有出错,这是默认值。

回答by KevinM

Since you don't want to delete all Terminal windows, first change the name of your window from "Terminal" to something else:

由于您不想删除所有终端窗口,首先将窗口名称从“终端”更改为其他名称:

echo -n -e "\033]0;My Window Name\007"

echo -n -e "\033]0;我的窗口名称\007"

Then at the end of the script, use:

然后在脚本的末尾,使用:

osascript -e 'tell application "Terminal" to close (every window whose name contains "My Window Name")' &

osascript -e '告诉应用程序“终端”关闭(名称包含“我的窗口名称”的每个窗口)'&

回答by jaypal singh

You can use apple scriptto quitthe terminal app. Add the following to your script -

您可以使用apple script退出terminal app。将以下内容添加到您的脚本中 -

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

This will give you a popup confirming to close the app. You can disable this in Terminal preferences.

这将为您提供一个确认关闭应用程序的弹出窗口。您可以在终端首选项中禁用此功能。

Alternatively, you can also use killallcommand to quit the app. The following would work just as well.

或者,您也可以使用killall命令退出应用程序。以下也同样有效。

killall Terminal

Note:

笔记:

Just as a side note, you can freely add the above commands to your script and it would work as you want. However, there are few caveats. Firstbeing you will limit the ability of your script to work on different boxes. Secondly, it would be safer to use nohupso that any commands that are currently running won't quit due to quitting of the Terminal app.

作为旁注,您可以自由地将上述命令添加到您的脚本中,它会按您的意愿工作。但是,有几个警告。首先,您将限制脚本在不同盒子上工作的能力。其次,使用会更安全,nohup这样当前正在运行的任何命令都不会因退出Terminal app.

回答by Arron

This works for me:

这对我有用:

#!/bin/sh

{your script here}

osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit

This will work for closing just your windows opened with a .command file but leave things already running in other terminal windows. I know that I almost always have either sass or grunt watch something so I don't want to quit terminal totally.

这将仅用于关闭使用 .command 文件打开的窗口,但保留其他终端窗口中已经运行的内容。我知道我几乎总是有 sass 或 grunt 看一些东西,所以我不想完全退出终端。

回答by user2373121

closeWindow() {
    /usr/bin/osascript << _OSACLOSE_
    tell application "Terminal"
        close (every window whose name contains "YourScriptName")
    end tell
    delay 0.3
    tell application "System Events" to click UI element "Close" of sheet 1 of window 1 of application process "Terminal"
_OSACLOSE_
}

This will close the Terminal window for your script and keep any other Terminal windows open as long as their window titles don't match. For it to work Terminal will have to be added to the list of applications permitted to use the Accessibility framework. You can also cycle through Terminal windows with a repeat command and close every window x that contains a UI element "Close" on sheet 1.

这将关闭脚本的终端窗口并保持任何其他终端窗口打开,只要它们的窗口标题不匹配。要使其正常工作,终端必须添加到允许使用辅助功能框架的应用程序列表中。您还可以使用重复命令循环浏览终端窗口,并关闭工作表 1 上包含 UI 元素“关闭”的每个窗口 x。

回答by Gary Wisniewski

I find the best solution for this is to use Automator to create a true OSX application which will work the same way regardless of how your system is configured. You can have the Automator run your shell script, or you can embed the shell script itself in Automator.

我发现最好的解决方案是使用 Automator 创建一个真正的 OSX 应用程序,无论您的系统如何配置,它都会以相同的方式工作。您可以让 Automator 运行您的 shell 脚本,或者您可以将 shell 脚本本身嵌入到 Automator 中。

Here is how you do it:

这是你如何做到的:

  1. Run Automator (in Applications).
  2. Choose "New Document" and when it asks "Choose a type for your document" choose "Application"
  3. In the left panel, select "Utilities" then "Run Shell Script".
  4. Type in your script commands in the workflow item in the right panel. You can either call another shell script, or just put your commands in their directly.
  5. Save the Application, which will be a full-fledged Mac App. You can even cut-and-paste icons from other apps to give your script some personality.
  1. 运行 Automator(在应用程序中)。
  2. 选择“新建文档”,当它询问“为您的文档选择类型”时选择“应用程序”
  3. 在左侧面板中,选择“实用程序”,然后选择“运行 Shell 脚本”。
  4. 在右侧面板的工作流项目中输入您的脚本命令。您可以调用另一个 shell 脚本,也可以直接将命令放入它们中。
  5. 保存应用程序,这将是一个成熟的 Mac 应用程序。您甚至可以从其他应用程序中剪切和粘贴图标,为您的脚本赋予一些个性。