Thanasis Petsas
Thanasis Petsas

Reputation: 4448

how to attach to screen sessions within a loop?

I use a bash script to spawn screen sessions in "detached" mode (using -d -m options) and name them (through -S ) and then I attach to them to give some commands (through -r ):

#!/bin/bash
screen -d -m -S session_name_1
screen -d -m -S session_name_2
screen -d -m -S session_name_3
...
screen -r session_name_1
screen -r session_name_2
screen -r session_name_3

when I do the whole process in a loop fashion I cannot attach to a screen (the file session_names.txt is a file whose each line contains a session name):

#!/bin/bash
while read line; do
  echo $line
  screen -d -m -S $line
done < session_names.txt

while read line; do
  echo $line
  screen -r $line
done < session_names.txt

I can't attach to a screen and this error occurs:

"Must be connected to a terminal."

How I can overcome this problem and why this problem occurs?

Upvotes: 3

Views: 3025

Answers (1)

Karoly Horvath
Karoly Horvath

Reputation: 96258

In the whole loop the standard input comes from the txt file, so screen is not seeing the terminal.

This should do it, but note that 'line' really means 'word' here.

for line in `cat session_names.txt`; do
  echo $line
  screen -r $line
done

Upvotes: 3

Related Questions