Shell script run 1 session with multiple windows

I am new to shell scripting (you can say I'm just starting). I need to write a shell script to open ONLY 1 β€œscreen” session. Then I want to open several windows (for example, 10) in one session and each session should do something, for example, type "hello". So, here is part of my code, but it creates only one window (0) and does not print anything in this window:

#!/bin/bash screen-d -m -S mysession for n in {1..10}; do i=$(($n-1)) screen -S mysession -p $i -X echo "hello" done 

As I said, my sample code does not work! It opens one session with only one window "0", and nothing is printed on the terminal in the window "0".

could you help me? The code should open one screen session, and then in a cycle open 10 windows and type β€œhello” in each window.

Thank you in advance!

Abedin

+8
bash shell gnu-screen
source share
1 answer

The command you can send with the -X option is not a shell command, but a screen command.

Check the β€œSETUP” section of man screen for a list of screen commands. the following code uses the screen command to create a new window and the stuff command to display text in the window.

 #!/bin/bash screen -d -m -S mysession # window 0 is created by default, show hello0 on it screen -S mysession -p 0 -X stuff hello0 for n in {1..9}; do # create now window using `screen` command screen -S mysession -X screen $n screen -S mysession -p $n -X stuff hello$n done 

Now you can connect to the myscreen session and check that there are 10 windows and hello0 .. hello9 is displayed in each window.

 $ screen -r mysession [Press Ca "] 
+9
source share

All Articles