Score:0

How to run a command within a previously opened terminal with gnome-terminal command in a shell script?

cn flag

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.

Doug Smythies avatar
gn flag
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.
sudodus avatar
jp flag
It is also possible to communicate via the existence of a file.
N. S. avatar
cn flag
@sudodus. What file do you mean?!
sudodus avatar
jp flag
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.
N. S. avatar
cn flag
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.
raj avatar
cn flag
raj
Must there be 4 separate `gnome-terminal` windows? Or can there be a single window with 4 separate, switchable terminal sessions? Because in the latter case you can use an `expect` script combined with `screen` (where you start 4 sessions within `screen`) to get the desired behavior.
Score:2
cn flag

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.

N. S. avatar
cn flag
I've edited my question and added extra explanations. Thanks for taking a look...
Score:1
jp flag

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 "
N. S. avatar
cn flag
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.
sudodus avatar
jp flag
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.
N. S. avatar
cn flag
How can I tell only to you? Thanks for your help
sudodus avatar
jp flag
@N.S. You can get a user ID at the [Ubuntu Forums](https://ubuntuforums.org) and communicate via Ubuntu Forums messages (My user ID is 'sudodus' there too).
N. S. avatar
cn flag
I'm so sorry. I don't know how to communicate with you via Ubuntu Forums! :(
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.