0

I want to run a source code that needs four terminal A, B, C and D. There are some commands for source code running in Ubuntu. These commands must be run in order in terminals A, B, C, D, A, B and C. Indeed these commands are interconnected and must be executed in this specific order.

I want to automatically run the source code with shell scripting. I already have wrote a shell script that opens multiple terminals with gnome-terminal and it works well so far:

gnome-terminal --title="A" -- bash -c "cd ~; ./myscript1; exec bash"
gnome-terminal --title="B" -- bash -c "cd ~; ./myscript2; exec bash"
gnome-terminal --title="C" -- bash -c "cd ~; ./myscript3; exec bash"
gnome-terminal --title="D" -- bash -c "cd ~; ./myscript4; exec bash"

Now I want back to terminal A and run another command within it.

The following statement does not work fine, it opens a new terminal!

gnome-terminal --title="A" -- bash -c "cd ~; command; exec bash"

I did not understand how to do this after reading man pages of Gnome-terminal and searching in the web.

6
  • 1
    You could pass a token from myscript3 to myscript1, say via a named pipe. After doing everything else, myscript1 would wait for the token and myscript3 would only send it at the appropriate time. Nov 2, 2021 at 18:44
  • It is also possible to communicate via the existence of a file.
    – sudodus
    Nov 3, 2021 at 11:45
  • @sudodus. What file do you mean?!
    – N. S.
    Nov 3, 2021 at 19:58
  • You can let the process in one terminal window create a file and check for it repeatedly, and let a process in another window remove it, when it is ready. Or the opposite: let the process in one terminal window wait until a file exists, and let a process in another window create that file, when it is ready. The filename could be a temporary file (or a set of temporary files), and the name(s) can be controlled by a master shellscript, that initiates the processes in the four terminal windows. - Such a file can be empty or contain a message.
    – sudodus
    Nov 3, 2021 at 20:04
  • I have created 2 bash script "A" and "B", and a "run" script that runs "A" and "B" in parallel. "run" content is: bash ./A & bash ./B & wait. I wait in "A" for a file creation with a "while" statement, and in "B" I create that file, but no commands from "A" are executed.
    – N. S.
    Nov 4, 2021 at 10:07

2 Answers 2

2

You can't, except perhaps on a hackish way using keypress simulation. e.g. xdotool to switch back to the first window, then paste some commands.

There may be better ways to do what you want. However, in order for us to give better suggestions, we need to know what you really want to achieve.

1
  • I've edited my question and added extra explanations. Thanks for taking a look...
    – N. S.
    Nov 3, 2021 at 10:57
1

Try with the following demo running A and B

A master script creates a temporary file (in /tmp) and starts a-script and b-script in xterm windows.

Install

sudo apt install xterm

Put all the scripts into a dedicated directory, make them executable and run

./master

master:

#!/bin/bash

echo "$0 start"

b4a=$(mktemp)

nohup xterm -fa default -e ./a-script "$b4a" 2> /dev/null &
nohup xterm -fa default -e ./b-script "$b4a" 2> /dev/null &

if test -f "$b4a"
then
 rm "$b4a"
fi
echo "$0 finish"

a-script:

#!/bin/bash

echo "$0 start"
echo "$0 can do things here ..."
while ! test -f "$1"
do
 sleep 1
 echo -n "."
done
echo ""
echo "$0 can do things here ..."
rm "$1"
read -p "$0: Press enter to finish "

b-script:

#!/bin/bash

echo "$0 start"
echo "$0 can do things here ..."
sleep 5
touch "$1"
read -p "$0: Press enter to finish "
5
  • Thanks for your answer. I already wrote these scripts, but I think the problem is related to my docker containers! Indeed I run two containers in a-script and b-script and after that I want to run some commands in these containers. When I write some commands in a-script after while loop, those commands will not execute in the corresponding container.
    – N. S.
    Nov 6, 2021 at 18:08
  • Why are you running two containers in a-script and b-script? And why are you using four terminal windows? -- It is probably possible to set up the structure of the task in a simpler way, that will do the work with less problems. If you tell us (or only me, if you don't want to make the task public), you can get help with the structure.
    – sudodus
    Nov 6, 2021 at 18:35
  • How can I tell only to you? Thanks for your help
    – N. S.
    Nov 6, 2021 at 18:59
  • @N.S. You can get a user ID at the Ubuntu Forums and communicate via Ubuntu Forums messages (My user ID is 'sudodus' there too).
    – sudodus
    Nov 6, 2021 at 20:04
  • I'm so sorry. I don't know how to communicate with you via Ubuntu Forums! :(
    – N. S.
    Nov 7, 2021 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.