bash 如何启动多个 xterm 窗口并在每个窗口上运行命令,然后打开每个窗口?

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

How can I launch multiple xterm windows and run a command on each, leaving each window open afterward?

linuxbashunixsshxterm

提问by user3609282

I'm lazy, and I prefer that computers do my work for me. I ssh into several machines on a daily basis, so I created a simple script that launches some xterm windows and places them in positions I want (as you can see, I'm using bash):

我很懒惰,我更喜欢计算机为我做我的工作。我每天通过 ssh 连接到几台机器,所以我创建了一个简单的脚本来启动一些 xterm 窗口并将它们放在我想要的位置(如你所见,我正在使用 bash):

#!/bin/bash
xterm -geometry 80x27+1930+0 &
xterm -geometry 80x27+2753+0 &
xterm -geometry 80x27+1930+626 &
xterm -geometry 80x27+2753+626 &

However, the next thing I do is go to the first window and type in

但是,接下来我要做的是转到第一个窗口并输入

ssh server_a

then in the second

然后在第二个

ssh server_b

and so on. What I'd like to do is have my script do the ssh commands in each xterm window, and then leave the windows open for me to do my work. I've seen the -e option for xterm, but the window closes after I execute my command. Is there a way to do this?

等等。我想要做的是让我的脚本在每个 xterm 窗口中执行 ssh 命令,然后让窗口保持打开状态让我完成我的工作。我已经看到 xterm 的 -e 选项,但在我执行命令后窗口关闭。有没有办法做到这一点?

I apologize if this is a duplicate question. I've searched around and haven't had any luck with this. Many thanks!

如果这是一个重复的问题,我深表歉意。我已经四处搜索,但没有任何运气。非常感谢!

采纳答案by Politank-Z

I'd love to see a more elegant answer, but what I came up with does work:

我很想看到一个更优雅的答案,但我想出的确实有效:

xterm -e bash -c 'echo foo; exec bash'

Replace echo foowith the command of your choice, and you're good to go.

替换echo foo为您选择的命令,您就可以开始了。

回答by Brian Vandenberg

This answergives one of the best answers I've seen so far to do this. Use the bash --init-fileflag either in the shebang or when executing the terminal:

这个答案给出了迄今为止我见过的最好的答案之一。--init-file在 shebang 中或在执行终端时使用 bash标志:

#!/bin/bash --init-file
commands to run

... and execute it as:

...并执行它:

xterm -e /path/to/script
# or
gnome-terminal -e /path/to/script
# or
the-terminal -e bash --init-file /path/to/script/with/no/shebang

My only real complaint with the execoption is if the command executed prior to exec bashis long running and the user interrupts it (^C), it doesn't run the shell. With the --init-file option the shell continues running.

我对这个exec选项唯一真正的抱怨是,如果之前执行的命令exec bash长时间运行并且用户中断它 ( ^C),它不会运行 shell。使用 --init-file 选项,shell 将继续运行。

Another option is cmdtoolfrom the OpenWin project:

另一种选择cmdtool来自 OpenWin 项目:

/usr/openwin/bin/cmdtool -I 'commands; here'
# or
/usr/openwin/bin/cmdtool -I 'commands; here' /bin/bash

... where cmdtoolinjects the commands passed with -Ito the slave process as though it was typed by the user. This has the effect of leaving the executed commands in the shell history.

... 其中cmdtool注入传递-I给从属进程的命令,就好像它是由用户键入的一样。这具有将执行的命令留在 shell 历史记录中的效果。

回答by theWebalyst

Another option is to use gnome terminator. This creates and positions terminals interactively, and you can set up each terminal to run commands within terminator preferences.

另一种选择是使用 gnome 终止符。这以交互方式创建和定位终端,您可以设置每个终端以在终止器首选项中运行命令。

Also does lots of extra tricks using keybindings for things like move, rotate, maximise/minimise of terminals within the containing terminator window

还使用键绑定进行了许多额外的技巧,例如移动、旋转、最大化/最小化包含终止符窗口内的终端

See: https://superuser.com/a/610048

参见:https: //superuser.com/a/610048

回答by Yariv

"ClusterSSH controls a number of xterm windows via a single graphical console window to allow commands to be interactively run on multiple servers over an ssh connection"

“ClusterSSH 通过单个图形控制台窗口控制多个 xterm 窗口,以允许通过 ssh 连接在多个服务器上交互式运行命令”

https://github.com/duncs/clusterssh/wiki

https://github.com/duncs/clusterssh/wiki

$ cssh server_a server_b
$ command