本文介绍了避免在脚本执行后关闭 gnome-terminal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 bash 脚本,可以打开多个 gnome 终端,通过 ssh 连接到教室计算机并运行脚本.

I created a bash script that opens several gnome-terminals, connect to classroom computers via ssh and run a script.

如何避免脚本完成后 gnome-terminal 关闭?请注意,我还希望能够在终端中输入更多命令.

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 -e "ssh root@<ip> cd /tmp && ls"

推荐答案

据我所知,您希望 gnome-terminal 打开,让它执行一些命令,然后回到提示符处,以便您可以输入更多命令.Gnome-terminal 不是为这个用例设计的,但有解决方法:

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 -e "bash -c "echo foo; echo bar; exec bash""

最后的 exec bash 是必要的,因为 bash -c 将在命令完成后终止.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.

准备somercfile:

source ~/.bashrc
echo foo
echo bar

然后运行:

$ gnome-terminal -e "bash --rcfile somercfile"

让 gnome-terminal 运行一个脚本来运行你的命令,然后进入 bash

准备scripttobash:

#!/bin/sh
echo foo
echo bar
exec bash

将此文件设置为可执行文件.

Set this file as executable.

然后运行:

$ gnome-terminal -e "./scripttobash"

或者你可以制作一个genericscripttobash:

#!/bin/sh
for command in "$@"; do
  $command
done
exec bash

然后运行:

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

说了这么多,这可能对你有用:http://www.linux.com/archive/feature/151340

All that said, this might be of good use for you: http://www.linux.com/archive/feature/151340

这篇关于避免在脚本执行后关闭 gnome-terminal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 07:33