Title: How to attach gnu screen without knowing tty number
Tags: bash;gnu-screen
Question: After starting multiple gnu screen sessions with
screen -S name1
screen -S name2
I would like to selectively attach to them. If I do
screen -D -R name1
it will not attach but instead print:
There are several suitable screens on:
6799.name1        (08.02.2013 15:47:23)   (Detached)
3661.name2        (08.02.2013 13:43:25)   (Detached)
Type "screen [-d] -r [pid.]tty.host" to resume one of them.
It works obviously if I type:
screen -D -R 6799.name1
But I would like to attach without knowing the tty number, because I want to start the screen sessions by a script which takes the name as argument. Not knowing the tty number at this time I would need to copy by hand this number whenever I want to reattach later.
An obvious solution would be to extract the tty number from the above screen output using string operations. But that seems a little awkward. Is there a way to tell screen to attach to a specific named session, whatever the tty number is?
Note that I tried the different [-d|-D] [-r|-R] combinations mentioned in the man pages.
Note added:
I create the separate screen sessions in a script using ssh with
ssh -t ${host} "
screen -S \"$exefile\" -t \"$exefile\" -R "nice -n$prio \"$exefile\" ${exeparams[@]}";
exit 0";
Later I try to connect using
ssh -t ${host} "
screen -D -R -S \"$exefile\" &amp;&amp;
echo \"finished.\";
exit";
where $exefile is the name of the executable and the screen session I want to connect to.
Comment: I actually went for the awkward solution when I wrote a wrapper shell script around screen https://github.com/frostschutz/Screen-Manager - it has a sm_pidof() function that extracts the number for a given name. But it also needs that for other check (such as is the screen already running or what).

Here is the accepted answer: The only reliable solution I found was the brute force method outlined in the question. To connect to an existing screen with name $exefile on remote $host via ssh, or to create a new one if it does not exist, one may use (in a bash shell):
ssh -t $host "
echo \"done.\" &amp;&amp;
echo \"itentifying screen ...\" &amp;&amp;
myscreen=\`screen -list | sed -n \"s/^\\s*\\s\\([0-9]*\\.$exefile\\)\\s\\s*.*$/\\1/p\"\` &amp;&amp;
if [ "X\$myscreen" = "X" ]; then
echo \"no existing screen session found, creating new\";
screen -S \"$exefile\";
else
echo \"connecting to screen \$myscreen\";
screen -d -r \"\$myscreen\";
fi &amp;&amp;
echo \"finished.\";
exit";
The first run will create a new session on $host. If the screen is detached with Ctrl+a,d the ssh login shell closes. If the same command is issued again it will reconnect to the existing screen. Multiple screen sessions with different names $exefile are possible and can be reattached independently. The magic is done by sed which searches the output of screen -list for a name matching the pattern [tty].name. Note that the screen name might need to be escaped if it contains sed special characters.

Here is another answer: This should work:
screen -S name1 -D -R
Comment for this answer: @highsciguy, what do you mean by _it seems to depend on the tty numbers_? I can't seem to be able to make in _not_ work. Can you please tell how to reproduce?
Comment for this answer: @highsciguy, in which way does it not work? Does it fail to attach the session, fail to detach from the other terminal? Attach the wrong session? Does it report an error? What version of `screen` (I'm testing with the `4.1.0~20120320gitdb59704` package from Debian)
Comment for this answer: @highsciguy. And are you sure there aren't two screen sessions with the same session name (like `1234.vim` and `2345.vim`)?
Comment for this answer: Seems I accepted this answer too early. It worked at first, but it seems to depend on the tty numbers. Now it does not work, independently of whether the screen sessions are attached, detached etc.
Comment for this answer: Please see my comment on how I create the screen sessions. I am not sure what causes the problems but it seems to work only sometimes.
Comment for this answer: It will print a message of the sort mentioned above "There are several suitable screens on: ...". Screen version 4.00.03jw4 (FAU) 2-May-06 (current Ubuntu).

Here is another answer: Because I am the only one connecting to my screen sessions, and sometimes I want more than one console connected (a la multi display mode) to a session at a time, I use the -x switch.
# screen -S hosts
# screen -ls
There is a screen on:
12828.hosts     (Detached)
1 Socket in /var/run/screen/S-uther.
# screen -x hosts
From man 1 screen
-x   Attach  to a not detached screen session.
(Multi display mode).Screen refuses to attach
from within itself.  But when cascading multiple
screens, loops are  not  detected; take care.
Comment for this answer: This could be a solution, but usually there is too much output on my screen sessions for this to be practicable.

{'source': 'StackExchangeDataset', 'subset': 'unix.stackexchange.com', 'metadata': {'AnswerCount': 3, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:02/08/13', 'FavoriteCount': 1.0, 'PostTypeId': 1, 'Tags': '<bash><gnu-screen>', 'Title': 'How to attach gnu screen without knowing tty number', 'ViewCount': 2476, 'id': 64181, 'user_AboutMe': None, 'user_AccountId': 189914.0, 'user_DisplayName': 'highsciguy', 'user_Reputation': 2464.0, 'user_Views': 43.0, 'user_id': 18047.0}}