Linux 避免在脚本执行后关闭 gnome-terminal?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3512055/
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
Avoid gnome-terminal close after script execution?
提问by Marten Bauer
I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.
我创建了一个 bash 脚本,可以打开多个 gnome 终端,通过 ssh 连接到教室计算机并运行脚本。
How can I avoid that the gnome-terminal closes after the script is finished? Note that I also want to be able to enter further commands in the terminal.
如何避免脚本完成后 gnome-terminal 关闭?请注意,我还希望能够在终端中输入更多命令。
Here is an example of my code:
这是我的代码示例:
gnome-terminal -e "ssh root@<ip> cd /tmp && ls"
回答by sourcerebels
回答by Gilles 'SO- stop being evil'
Stack Overflow answer: the terminal closes when the command run inside it has finished, so you need to write a command that doesn't terminate immediately. For example, to leave the terminal window open until you press Enterin it:
gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
Super Useranswer: Create a profile in which the preference “Title and Command/When command exits” is set to “Hold the terminal open”. Invoke gnome-terminal with the
--window-with-profile
or--tab-with-profile
option to specify the terminal name.
堆栈溢出答案:终端在其中运行的命令完成时关闭,因此您需要编写一个不会立即终止的命令。例如,要让终端窗口保持打开状态,直到您按下Enter它:
gnome-terminal -e "ssh host 'cd /tmp && ls'; read line"
超级用户回答:创建一个配置文件,其中首选项“标题和命令/命令退出时”设置为“保持终端打开”。使用
--window-with-profile
或--tab-with-profile
选项调用 gnome-terminal以指定终端名称。
回答by lesmana
As I understand you want gnome-terminal to open, have it execute some commands, and then drop to the prompt so you can enter some more commands. Gnome-terminal is not designed for this use case, but there are workarounds:
据我所知,您希望 gnome-terminal 打开,让它执行一些命令,然后回到提示符处,以便您可以输入更多命令。Gnome-terminal 不是为此用例设计的,但有一些解决方法:
Let gnome-terminal run bash and tell bash to run your commands and then run bash
让 gnome-terminal 运行 bash 并告诉 bash 运行您的命令,然后运行 bash
$ gnome-terminal -e "bash -c \"echo foo; echo bar; exec bash\""
The exec bash
at the end is necessary because bash -c
will terminate once the commands are done. exec
causes the running process to be replaced by the new process, otherwise you will have two bash processes running.
在exec bash
因为到底是必要的bash -c
,一旦命令完成了将终止。exec
导致正在运行的进程被新进程替换,否则您将有两个 bash 进程在运行。
Let gnome-terminal run bash with a prepared rcfile
which runs your commands
让 gnome-terminal 运行 bash 并准备好rcfile
运行您的命令
Prepare somercfile
:
准备somercfile
:
source ~/.bashrc
echo foo
echo bar
Then run:
然后运行:
$ gnome-terminal -e "bash --rcfile somercfile"
Let gnome-terminal run a script which runs your commands and then drops to bash
让 gnome-terminal 运行一个脚本来运行你的命令,然后放到 bash
Prepare scripttobash
:
准备scripttobash
:
#!/bin/sh
echo foo
echo bar
exec bash
Set this file as executable.
将此文件设置为可执行文件。
Then run:
然后运行:
$ gnome-terminal -e "./scripttobash"
Alternatively you can make a genericscripttobash
:
或者你可以做一个genericscripttobash
:
#!/bin/sh
for command in "$@"; do
$command
done
exec bash
Then run:
然后运行:
$ gnome-terminal -e "./genericscripttobash \"echo foo\" \"echo bar\""
Every method has it's quirks. You must choose, but choose wisely. I like the first solution for its verbosity and the straightforwardness.
每种方法都有它的怪癖。你必须选择,但要明智地选择。我喜欢第一个解决方案的冗长和直接。
All that said, this might be of good use for you: http://www.linux.com/archive/feature/151340
说了这么多,这可能对你有用:http: //www.linux.com/archive/feature/151340
回答by Lukasz Frankowski
Finally this one works for me:
最后这个对我有用:
gnome-terminal --working-directory=WORK_DIR -x bash -c "COMMAND; bash"
回答by NickyP
If running a bash script just add gedit afile
to the end of the script and that will hold gnome-terminal open. "afile" could be a build log which it was in my case.
如果运行 bash 脚本,只需添加gedit afile
到脚本的末尾,这将使 gnome-terminal 保持打开状态。“afile”可能是一个构建日志,就我而言。
Did not try just using gedit
alone but, that would properly work too.
没有尝试gedit
单独使用,但是也可以正常工作。
回答by Zini
Run with -ic
instead -i
to make terminal close bash proccess when you close your terminal gui:
与运行-ic
,而不是-i
使终端接近的bash proccess当您关闭终端界面:
gnome-terminal -e "bash -ic \"echo foo; echo bar; exec bash\""
回答by Leo
The ideal solution would be to ask for a user input with echo "Press any key".
理想的解决方案是要求用户输入回声“按任意键”。
But if double-click in Nautis or Nemo and select run in a terminal, it doesn't seem to work.
但是如果在 Nautis 或 Nemo 中双击并选择在终端中运行,它似乎不起作用。
In case of Ubuntua shell designed for fast start-up and execution with only standard features is used, named dash I believe. Because of this the shebang is the very first line to start with to enable proper use of bash features. Normally this would be: #!/bin/bash or similar. In Ubuntu I learned this should be: #!/usr/bin/env bash.
在Ubuntu的情况下,使用了一个专为快速启动和执行而设计的 shell,它仅具有标准功能,我相信名为 dash。因此,shebang 是开始正确使用 bash 功能的第一行。通常这将是:#!/bin/bash 或类似的。在 Ubuntu 中,我了解到这应该是:#!/usr/bin/env bash。
Many workarounds exist to keep hold of the screen before the interpreter sees a syntax error in a bash command.
在解释器看到 bash 命令中的语法错误之前,存在许多解决方法来保持屏幕。
The solution in Ubuntu that worked for me:
Ubuntu 中对我有用的解决方案:
#!/usr/bin/env bash
your code
echo Press a key...
read -n1
回答by Member2017
As of January 2020, the -e
option in gnome-terminal
still runs properly but throws out the following warning:
截至 2020 年 1 月,该-e
选项gnome-terminal
仍可正常运行,但会抛出以下警告:
For -e
:
对于-e
:
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
# 选项“-e”已弃用,可能会在更高版本的 gnome-terminal 中删除。
# 使用“--”终止选项并在其后执行命令行。
Based on that information above, I confirmed that you can run the following two commands without receiving any warning messages:
根据上述信息,我确认您可以运行以下两个命令而不会收到任何警告消息:
$ gnome-terminal -- "./scripttobash"
$ gnome-terminal -- "./genericscripttobash \"echo foo\" \"echo bar\""
I hope this helps anyone else presently having this issue :)
我希望这可以帮助目前遇到此问题的其他人:)
回答by philipper
For a solution applicable to any terminal, there is a script that opens a terminal, runs the command specified and gives you back the prompt in that new terminal:
对于适用于任何终端的解决方案,有一个脚本可以打开终端,运行指定的命令并在新终端中返回提示: