在bash脚本中创建多个终端并继续执行命令?

在bash脚本中创建多个终端并继续执行命令?,bash,terminal,sh,xterm,Bash,Terminal,Sh,Xterm,我创建了一个bash脚本来打开多个终端。但是,在打开所有终端并运行第一个命令后,我无法访问它们并在我选择的终端上执行我想要的命令。例如,在我发布的代码运行之后,我想在标题为“node5”的终端上运行insert Hi,1命令。我可以这样做吗?我当前的代码如下: #!/bin/bash printf "$FBOLD\nPlease enter the port: $FREG" read invalue xterm -title "node1" -hold

我创建了一个bash脚本来打开多个终端。但是,在打开所有终端并运行第一个命令后,我无法访问它们并在我选择的终端上执行我想要的命令。例如,在我发布的代码运行之后,我想在标题为“node5”的终端上运行
insert Hi,1
命令。我可以这样做吗?我当前的代码如下:

#!/bin/bash
printf "$FBOLD\nPlease enter the port:  $FREG"
read invalue
xterm -title "node1" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node2" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node3" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node4" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node5" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node6" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node7" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node8" -hold -e "node index.js localhost:${invalue}" &
sleep 1
xterm -title "node9" -hold -e "node index.js localhost:${invalue}" &
sleep 1

编辑:更清晰。我希望通过bash脚本执行命令,因为我希望在每个终端中执行数百次插入。因此,该过程应该是自动化的。

您可以使用xdotool实现自动化,下面是一个需要调整的示例:

#!/bin/bash
  
xterm -title node1 -hold -e 'bash -c "read; echo REPLY=\$REPLY"' &
sleep 1

xdotool windowfocus --sync $(xdotool search --name node1) # set focus on xterm with title `node1`
xdotool type "Hello, World!"
xdotool key Return

如果您在ubuntu上,请通过
apt Install xdool
安装。

难道您没有一个鼠标,可以“点击”表示“标题节点终端”的窗口吗?@KamilCuk是的,但我想做数百次插入,所以我想自动化这个过程为什么您首先要使用
xterm
?为什么不做一些你可以真正自动化的事情呢?像
screen
tmux
@KamilCuk,我不知道怎么做。你能写一个答案吗?我搜索了很多,但都没有找到,但是,你不能运行
echo“insert Hi,1”| node….
?是否有任何理由需要运行交互式shell?为什么不以非交互方式运行它呢?非常感谢!