我如何在 BASH 脚本的新终端中启动命令

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

how do i start commands in new terminals in BASH script

bash

提问by fightermagethief

my bash script reads in a few variables and then runs airodump on my home network. I would like to keep the window with airodump running and open some new terminals to run other commands for network discovery while the airodump screen is open (so i can see the results).
right now what i have looks like this (edited for brevity):

我的 bash 脚本读入了几个变量,然后在我的家庭网络上运行 airodump。我想保持 airodump 运行的窗口并打开一些新终端以在 airodump 屏幕打开时运行其他命令以进行网络发现(以便我可以看到结果)。
现在我看起来像这样(为简洁起见进行了编辑):

#!/bin/bash
read -p "Enter the channel: $channel"
airomon-ng start wlan0 $channel,$channel
airodump-ng -c $channel,$channel mon0 &&
terminator -e netstat -ax &&
terminator -e nmap 192.168.1.1

the first command that uses the whole terminal (airodump) starts up fine and i can see the network, but then it just stays on that screen. If i ctrl+c then it goes back to prompt but i can see the error : Usage: terminator [options] error no such option
i want the netstat and nmap commands to appear and stay in their own terminal windows, how can i do this?

使用整个终端 (airodump) 的第一个命令启动正常,我可以看到网络,但它只是停留在该屏幕上。如果我 ctrl+c 然后它返回到提示但我可以看到错误:用法:终止符[选项]错误没有这样的选项
我希望netstat和nmap命令出现并留在他们自己的终端窗口中,我该怎么做?

回答by zwol

The terminal window is generated by a separate program from the command running inside. Try one of these variations:

终端窗口由与内部运行的命令不同的程序生成。尝试以下变体之一:

xterm -e airomon-start wlan0 "$channel","$channel" &

gnome-terminal -x airomon-start wlan0 "$channel","$channel" &

konsole -e airomon-start wlan0 "$channel","$channel" &

Pick the command that invokes the terminal program you like. You'll have to do this for everycommand that you want run in its own window. Also, you need to use a single &at the end of each such command line -- not a double &&-- those do totally different things. And the last line of your script should be just

选择调用您喜欢的终端程序的命令。您必须为要在其自己的窗口中运行的每个命令执行此操作。此外,您需要&在每个此类命令行的末尾使用一个单引号——而不是双引号&&——它们会做完全不同的事情。你的脚本的最后一行应该只是

wait

that makes it not exit out from under all the terminals, possibly causing them all to die.

这使得它不会从所有终端下退出,可能导致它们全部死亡。

Obligatory tangential shell-scripting nitpick: ALWAYS put shell variable uses inside double quotes, unless you know for a factthat you need word-splitting to happen on a particular use.

强制性切向 shell 脚本 nitpick:始终将 shell 变量使用放在双引号内,除非您知道在特定用途上需要进行分词。

回答by Ernest Friedman-Hill

If the script is running in the foreground in the terminal, then it will pause while an interactive command is using the terminal. You could change the script to run airodump in a new terminal as well, or you could run the background commands beforeyou start airodump (maybe after sleeping, if you're concerned they won't work right if run first?)

如果脚本在终端的前台运行,那么它会在交互式命令使用终端时暂停。您也可以更改脚本以在新终端中运行 airodump,或者您可以启动 airodump之前运行后台命令(也许在睡眠之后,如果您担心如果先运行它们将无法正常工作?)