【问题标题】:Creating multiple terminals in bash script and keep executing commands?在 bash 脚本中创建多个终端并继续执行命令?
【发布时间】:2021-04-06 09:59:48
【问题描述】:

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

【问题讨论】:

  • 难道你没有一个鼠标,你可以在代表“带有标题 node5 的终端”的窗口上“单击”吗?
  • @KamilCuk 是的,但我想做数百个插入,所以我想自动化这个过程
  • 你为什么首先使用xterm?为什么不是你可以真正自动化的东西?喜欢screentmux
  • @KamilCuk 我不知道该怎么做。你能写一个关于它的答案吗?我搜索了很多,但没有出现
  • 但是,你还是不能运行echo "insert Hi, 1" | node .... 吗?您是否有任何理由需要运行 interactive shell?为什么不以非交互方式运行它?

标签: bash terminal sh xterm


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2015-04-24
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 2014-12-10
    • 1970-01-01
    • 2014-05-04
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多