bash - 在 bash 脚本中创建多个终端并继续执行命令?
问题描述
我创建了一个 bash 脚本来打开多个终端。但是,在打开所有终端并运行第一个命令后,我无法访问它们并在我选择的终端上执行我想要的命令。例如,在我发布的代码运行后,我想insert Hi, 1
在终端上运行标题为“node5”的命令。我可以这样做吗?我当前的代码如下:
#!/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
apt install xdotool
如果您在 ubuntu 上,请安装。
推荐阅读
- ios - 在 TestFlight 中看不到构建
- c# - 映射子字段问题
- sql - 返回除当前月份以外的所有数据
- c - 在头文件中使用结构时定义不完整
- algorithm - 鉴于我可以在多达 10 ^ 6 次计算中完成此任务,如何获得所有因素,即 10 ^ 18 范围内的除数?
- excel - 如何在同一单元格中查找重复项并在 VBA 中删除一个实例
- powershell - Powershell 脚本从 CSV 文件中删除不需要的引号
- spring-boot - 如何将 QueryByExampleExecutor 与 spring-data-elasticsearch 一起使用
- maven - Maven 在依赖项中查找文件
- python - 更改 Pandas 数据框列中的数据类型