有没有一种方法可以在shell脚本中关闭终端窗口? 我有一个.command文件,一旦完成任务就应该消失不见。
使用exit 0
会干净地终止脚本。
终端窗口是否保持打开由用户配置。默认情况下,始终保持打开。要更改此设置:
Terminal.app > Preferences > Profiles > Shell
- "When the shell exists:"
> Close if the shell exited cleanly
- "Ask before closing:"
(•) Never
-- OR --
(•) Only if there are....
当使用“如果Shell干净退出则关闭”选项时,如果退出结果为0(如果没有出现错误,则默认为0),脚本将关闭窗口。
.command
会在shell中添加;exit
),但你可以调用一个不关闭的AppleScript,例如:osascript -e 'tell application "Terminal"' -e activate -e 'do script "run.my.jobs"' -e 'end tell'
-- 那个窗口将始终保持打开状态。 - Simon Urbanek由于您不想删除所有终端窗口,首先将窗口名称从“Terminal”更改为其他名称:
echo -n -e "\033]0;我的窗口名称\007"
然后在脚本末尾使用:
osascript -e 'tell application "Terminal" to close (every window whose name contains "我的窗口名称")' &
AppleScript
来 退出 终端应用程序
。将以下内容添加到你的脚本中 -osascript -e 'tell application "Terminal" to quit'
这将会弹出一个确认窗口来关闭应用。您可以在终端偏好设置中禁用此功能。
或者,您也可以使用killall
命令退出应用程序。以下方法同样适用。
killall Terminal
顺便提一下,您可以自由地将上述命令添加到脚本中,以使其按照您的意愿工作。但是,有几个需要注意的问题。首先,这样会限制脚本在不同设备上的能力。其次,最好使用 nohup
,以便正在运行的任何命令都不会因为退出终端应用程序
而停止。
apple scripts
。但是,我真的不知道它是osascript
命令。然而,还有另一个很酷的命令叫做killall
,它有点类似。谢谢你的信任和赞成。非常感激。 :-) - jaypal singhexit 0
(或什么也不写)即可。 - Simon Urbanek这对我有效:
#!/bin/sh
{your script here}
osascript -e 'tell application "Terminal" to close (every window whose name contains ".command")' &
exit
这将适用于关闭仅使用 .command 文件打开的窗口,但是会保留其他终端窗口中已运行的程序。 我知道我几乎总是要么有 sass 或 grunt 在监视着某些东西,所以我不想完全退出终端。
osascript -e 'tell app "Terminal"' -e 'close (every window whose name contains ".command")' -e 'if number of windows = 0 then quit' -e 'end tell' & exit;
。多个-e
在脚本中添加换行符;对我来说,"\n"
存在问题。 - BeejorcloseWindow() {
/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_
}
#!/bin/bash -x
{your script here}
. exit 0
kill -9 $PPID
你也可以为你的脚本创建一个快捷方式:
cp yourscript.sh ~/bin/yourshortcutnamewhateveryouwant
然后输入
yourshortcutnamewhateveryouwant
将运行在任何目录中编写的脚本。
exit -f
对你不起作用吗?如果您不想让正在运行的命令突然退出,可以使用nohup
。 - jaypal singh