-2

I have created a bash script to open multiple terminals. However, after all the terminals are opened and have run the first command, I cannot access them and execute the commands I want on the terminal I choose. For example after the code I posted runs, I want to run the insert Hi, 1 command on terminal with title "node5". Can I do that? My current code is the following:

#!/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

Edit: To be clearer. I want to execute the commands throught the bash script, because I want to do hundreds of inserts in each terminal. So the process should be automated.

  • Don't you have, like, a mouse that you can "click" on the window that represents "terminal with title node5"? – KamilCuk Dec 29 '20 at 20:02
  • @KamilCuk yes but I want to do like hundreds of inserts, so I would like to automate the process – asimplecoder Dec 29 '20 at 20:04
  • why are you using `xterm` in the first place? Why not something that you can actually _automate_? Like `screen` or `tmux`. – KamilCuk Dec 29 '20 at 20:28
  • @KamilCuk I don't know how to do that. Can you write an answer about it? I searched a lot and those did not come up – asimplecoder Dec 29 '20 at 20:29
  • But, still, can't you just run `echo "insert Hi, 1" | node ....` ? Is there any reason you need to run an _interactive_ shell? Why not run it non-interactively? – KamilCuk Dec 29 '20 at 20:35
  • @KamilCuk I don't understand what you mean with this command. However, I need for all the terminals to have been created and to have run the first command. Then I want to run the insert commands on each of them – asimplecoder Dec 29 '20 at 20:37
  • @KamilCuk yes I can run a non-interactive shell, it doesn't matter because I want to run everything through a script. (I want only the port number as input) – asimplecoder Dec 29 '20 at 20:38
  • What is the problem you're trying to solve? Read in some port number and then run some command with that? – mjlitz Dec 29 '20 at 20:43
  • @mjiltz I have a file called insert.txt which has some insert commands like the one I have written in the question. The insert commands run through a DHT chord. So I want first to connect every server with the commands in the script I posted. Then parse the insert.txt file and run the insert commands one by one (on a random server in the network that I have created). I have managed to create the 10 terminal windows and create the servers and connect them with the script I posted. However I cannot run another command on each terminal that I have created. – asimplecoder Dec 29 '20 at 20:46

1 Answers1

0

You can use xdotool to automate, here is an example to adapt:

#!/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

Install by apt install xdotool if you are on ubuntu.

Philippe
  • 20,025
  • 2
  • 23
  • 32