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

问题描述

我创建了一个 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 xdotool 安装。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...