bash 如何创建新的终端会话并执行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/11500649/
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 create a new terminal session and execute multiple commands
提问by TheMeaningfulEngineer
I'm looking for a way to automate the start-up of my development environment. I have three virtual machines that have to be started, then have to sshto each of them and open VPN on them.
我正在寻找一种方法来自动启动我的开发环境。我有三个必须启动的虚拟机,然后必须ssh在每个虚拟机上打开 VPN。
So far I've gotten them to start and managed to ssh to them:
到目前为止,我已经让他们开始并设法 ssh 到他们:
#!/bin/sh 
virsh start virtual_1
virsh start virtual_2
virsh start virtual_3
sleep 2m
gnome-terminal --title "virtual_3: server" -x ssh [email protected] &
gnome-terminal --title "virtual_2: 11.100" -x ssh [email protected] &
gnome-terminal --title "virtual_1: 12.100" -x ssh [email protected] &
How do I execute an additional command in each of the terminals which starts openvpn?
如何在每个启动 openvpn 的终端中执行附加命令?
For simplicity I'm trying to echo 1in each terminal instead of starting VPN.
为简单起见,我试图echo 1在每个终端中而不是启动 VPN。
I've found that multiple commands on terminal start can be run like:
我发现终端启动时的多个命令可以像这样运行:
gnome-terminal -x bash -c "cmd1; cmd2"
So for one terminal to keep it simple I changed:
所以为了让一个终端保持简单,我改变了:
gnome-terminal --title "virtual_3: server" -x ssh [email protected] &
to:
到:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh [email protected] ; echo 1" &
But 1wasn't printed in the terminal of virtual_3.
Then I thought, maybe the command is being executed too quickly, before the terminal is ready, so I tried adding &&:
但是1没有打印在virtual_3的终端中。然后我想,也许命令执行得太快了,在终端准备好之前,所以我尝试添加&&:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh [email protected] &&; echo 1" &
But that gave no result either.
但这也没有结果。
回答by Shahbaz
First of all, if you run
首先,如果你运行
gnome-terminal -x bash -c "cmd1; cmd2"
you get bashto execute cmd1and cmd2. It doesn't first execute cmd1and then give cmd2to its result. sshis a program run in the terminal and your cmd2won't be executed until that is finished.
你可以bash执行cmd1和cmd2。它不会首先执行cmd1然后给出cmd2结果。ssh是在终端中运行的程序,在cmd2完成之前不会执行。
So you need to run sshand tell thatto execute your command.
所以你需要运行ssh并告诉它执行你的命令。
You can do so by:
你可以这样做:
ssh user@address "command_to_execute"
However, sshexits after the command is finished. As you can see in "With ssh, how can you run a command on the remote machine without exiting?", you can execute sshwith the -toption so it doesn't quit:
但是,ssh命令完成后退出。正如您在“使用 ssh,如何在不退出的情况下在远程机器上运行命令?”中看到的那样,您可以ssh使用该-t选项执行,这样它就不会退出:
ssh -t user@address "command_to_execute"
So your command in the end becomes:
所以你的命令最终变成:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh -t [email protected] 'echo 1'"
You are right, giving -talone is not enough (although necessary). -tallocates buffer for the ttybut doesn't execute bash for you. From the manual of ssh:
你是对的,-t仅仅给予是不够的(尽管必要)。-t为 分配缓冲区tty但不为您执行 bash。从手册ssh:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
If
commandis specified, it is executed on the remote host instead of a login shell.
-t 强制伪 tty 分配。这可用于在远程机器上执行任意基于屏幕的程序,这非常有用,例如在实现菜单服务时。多个 -t 选项强制 tty 分配,即使 ssh 没有本地 tty。
如果
command指定,它将在远程主机上执行,而不是在登录 shell 上执行。
So what you need is, to execute bash your self. Therefore:
所以你需要的是,执行 bash 你自己。所以:
ssh -t user@address "command_to_execute; bash"

