5

[ This can be considered step 2 of my previous question Is it possible to change GNU screen session name after created? ]

Actually, I'd like to write a script that can display current screen session name and change current session name.

For example:

sren armcross

It will change the session name to armcross (ARM gcc cross compiler) and output something like:

screen session name changed from '25278.pts-15.linux-ic37' to 'armcross'

So, the key question now is how to get current session name. Not only for display the old session name, but according to Is it possible to change GNU screen session name after created? , I have to know it(pass to -d -r) before I can change it to something else.

Can we use $STY for current session name? No. $STY will not change after you have changed the session name to a user-defined one. However, for command

screen -d -r <oldsessname> -X sessionname armcross

<oldsessname> should be the user-defined name(if ever defined) instead of $STY, otherwise, screen spouts error "No screen session found."

Maybe, there is a verbose way, use screen -list to list all sessions(user-defined name listed), then, match the pid part from $STY against those listed sessions and we will find current session's user-defined name. It should not be so verbose for such a straightforward question. Don't you think so?

The -d -D and -r -R options seems to expose too much implementation detail to screen's user. It seems, to rename a session, you have to detach it, then do the rename, then reattach it. Right?

My env: opensuse 11.3, GNU screen 4.00.03 (FAU) 23-Oct-06

Thank you.

Jimm Chen
  • 1,709
  • 5
  • 17
  • 32

2 Answers2

3

could you try this one;

screen -d -r $(ps -o ppid -p $$ --no-headers) -X sessionname newsessionname

cikuraku
  • 166
  • 7
1

It appears that when you set the sessionname manually screen changes the name of the relevant file in /var/run/screen/S-$USER/ but doesn't update the value of $STY which would be ideal.

We can still use the original value of $STY to get the current session's name though as $STY contains the PID that relates to this sessionname.

PID=$(echo $STY | awk -F"." '{ print $1}')

You can then use the PID to get the sessionname from /var/run/screen/S-$USER

SESSIONNAME=$( ls /var/run/screen/S-$USER/$PID* | awk -F"." '{print $2}' )

Now you have the sessionname in SESSIONNAME you can do what you want with it.

user9517
  • 114,104
  • 20
  • 206
  • 289