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

时间:2020-12-29 19:41:54

标签: bash terminal sh xterm

我创建了一个 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 脚本执行命令,因为我想在每个终端中进行数百次插入。所以这个过程应该是自动化的。

1 个答案:

答案 0 :(得分:0)

你可以使用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 xdotool 安装。