1

Using "screen -D -R -S foo", one can attach to an existing session named "foo", or if said session doesn't exist, create it.

How does one also source a file that contains screen commands?

I thought that this would work:

screen -D -R -S foo -X source file

Unfortunately, that fails with this message:

No screen session found.

EDIT: As zebediah49 pointed out in a comment, I left out the "source" in "-X source file" by mistake. Updated now.

nickh
  • 4,721
  • 2
  • 29
  • 31
  • Could you not have it run the command `source $file` ? It's very possible it will only work if it is creating a new terminal though--running arbitrary "init" commands could end poorly in a window that's currently doing something. – zebediah49 Oct 26 '12 at 03:55
  • @zebediah49 You're right. I meant to include the "source" command in the question. The question's been updated now. Thanks. – nickh Oct 26 '12 at 13:34

1 Answers1

1

OK, from a close reading of the man page I note:

   -X   Send the specified command to a running screen  session.  You  can
        use  the  -d or -r option to tell screen to look only for attached
        or detached screen sessions. Note that this command  doesn't  work
        if the session is password protected.

running screen session. In other words, I don't believe you can do what you're looking for like that, with only one command. However, you can

  1. create the window if it does not exist
  2. send the command to the window
  3. connect to the window:

    NL=$'\n' NAME=foo screen -ls | grep "$NAME" || screen -d -m -S "$NAME" screen -r "$NAME" -X stuff "source file$NL" screen -D -R -S "$NAME"

(Clarification of how -X works, from Send commands to a GNU screen )

Community
  • 1
  • 1
zebediah49
  • 7,467
  • 1
  • 33
  • 50