bash 脚本退出后如何退出终端?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19881702/
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
How to quit terminal after script exits?
提问by nitishch
I wrote a simple script
我写了一个简单的脚本
#!/bin/bash
nohup &
exit
what it is supposed to do is if I execute 'scriptName' gedit
, it must open gedit and then close the terminal. But, problem is terminal is not closed after the script is executed.
它应该做的是如果我执行'scriptName' gedit
,它必须打开 gedit 然后关闭终端。但是,问题是脚本执行后终端没有关闭。
Things taken care of:
处理的事情:
- Checked terminal preferences to
When command exits: Exit the terminal
- I want to put this script in
/usr/local/bin
so, I cannot use execution like . ./scriptname - It appears like terminal is not the parent of this. How do I change this?
- 检查终端首选项
When command exits: Exit the terminal
- 我想把这个脚本放进去,
/usr/local/bin
所以我不能使用像 . ./脚本名 - 看起来终端不是它的父级。我该如何改变?
回答by pobrelkey
The approach you're trying won't work for the following reason:
由于以下原因,您尝试的方法不起作用:
The shell for your terminal session is one process; normally when you execute a shell script, the terminal session shell starts a second shell process and runs the script under that. So your exit
at the end of the script tells the second shell to terminate - which it would do anyway, as it's reached the end of the script. While you could try killing the first shell process from the second shell process (see comment about $PPID
in the other answer), this wouldn't be very good form.
终端会话的 shell 是一个进程;通常,当您执行 shell 脚本时,终端会话 shell 会启动第二个 shell 进程并在该进程下运行脚本。所以你exit
在脚本的末尾告诉第二个 shell 终止——无论如何它都会这样做,因为它已经到达了脚本的末尾。虽然您可以尝试从第二个 shell 进程中终止第一个 shell 进程(请参阅$PPID
另一个答案中的评论),但这不是很好的形式。
For your script to work the way you expect it to work, you'll need to get your terminal session shell to run the commands in your script by using bash's builtin source
command - type source /path/to/your/script gedit
, or . /path/to/your/script gedit
for short.
为了让您的脚本按照您期望的方式工作,您需要让终端会话 shell 使用 bash 的内置source
命令 - typesource /path/to/your/script gedit
或. /path/to/your/script gedit
简称来运行脚本中的命令。
Because of the way you'd need to execute this, putting the script on your PATH
wouldn't help - but you could create an alias (a "shortcut" that expands to a series of shell commands) to make running the script easier - add a line like alias your_alias_name='. /path/to/your/script'
to your ~/.bashrc
.
由于您需要执行此操作的方式,因此将脚本放在您的服务器上PATH
无济于事 - 但您可以创建一个别名(扩展为一系列 shell 命令的“快捷方式”)以使运行脚本更容易 - 添加一条线喜欢alias your_alias_name='. /path/to/your/script'
你的~/.bashrc
。
回答by Mark Setchell
I am presuming you are on a Mac - because you mention Preferences. The question is how did you start the script?
我假设您使用的是 Mac - 因为您提到了首选项。问题是你是如何启动脚本的?
If you started the Terminal and then ran "scriptName" from the Terminal, the Terminal won't quit because it is still running your shell. Check the Preferences closely, it says "When the shell exits", not "When the command exits".
如果您启动终端,然后从终端运行“scriptName”,终端将不会退出,因为它仍在运行您的 shell。仔细检查首选项,它显示“当 shell 退出时”,而不是“当命令退出时”。
回答by saji159
Given below the sample script.
下面给出了示例脚本。
#!/bin/bash
echo "hi"
/bin/bash/abc.sh &
sleep 2
#sleep is used to pause the execution temporary to see the output.
kill -25 $PPID
Value that pass to kill command determines the behavior. Hereyou can find the different values.
传递给 kill 命令的值决定了行为。在这里您可以找到不同的值。
Kill -25 worked for me to close the terminal and make sure to add "&"at the end of command. :)
Kill -25 为我关闭终端并确保在命令末尾添加“&”。:)