2

Possible Duplicate:
sending text input to a detached screen

With reference to the answered question: Sending input to a screen session from outside

I am trying to write a script that will create a screen and then stuff input (in my case, UNIX commands) into the shell that I want it to execute.

Here's what I have till now:

$ screen -dmS new_screen bash

$ screen -S new_screen -X stuff "cd /some/path/
some_script_file.sh
" <--This new line is required to execute the above command. 
$ # Note: See comments. We could insert the newline character instead of 'hard-coding' a newline string literal.

For some reason, the screen gets created fine and enters the bash shell. But the cd and some_script_file.sh commands don't get stuffed to the screen's bash shell.

Now the really strange part is.. After I detach from the screen, re-execute the stuff command as below..

$ screen -S new_screen -X stuff "cd /some/path/
some_script_file.sh
"

and then attach back into new_screen. Now I see that the commands were correctly stuffed and executed in the bash shell inside screen.

Not sure where I am going wrong here, there seems to be any issue stuff-ing commands into a not as yet attached screen.

Kent Pawar
  • 1,276
  • 4
  • 16
  • 37
  • 1
    In tmux you have to provide the ENTER word to signify you are done. Pretty sure you need to do the same with screen. – vgoff Nov 18 '12 at 06:05
  • @vgoff I guess that is handled when we add the newline character placing the double quotes on the new line. I guess an alternative way would be to write $ screen -S new_screen -X stuff "cd /some/path/^M some_script_file.sh^M" – Kent Pawar Nov 18 '12 at 06:25
  • Let me try this and get back to you... – Kent Pawar Nov 18 '12 at 06:25
  • @vgoff Yup it worked. I replaced the newline literals with the UNIX newline character (represented as ^M). This is done by opening the script in vi, then in insert mode I presses [CTRL] + [V] followed by [ENTER] to insert ^M (return key). – Kent Pawar Nov 18 '12 at 07:33

2 Answers2

3

When a screen session is first created, no window is selected. As such, when you send your commands, screen doesn't know which window to send it to. Once you have attached to the screen, however, the window has become selected, which is why you can send commands after doing so. To select a window from the command line you need to use the -p option.

Here's a modification of your command that should work:

screen -S new_screen -p 0 -X stuff "cd /some/path/
some_script_file.sh
"
Chris Down
  • 125,559
  • 25
  • 270
  • 266
0

@vgoff: "So a return literal inside the string. Would this work with pipes? Would you still need the return as the last character? – vgoff"

Looking at using a return literal or a return character (^M):

bash-3.00$ echo "(1) This sentence
> (2) has
> (3) 3 returns
> (4) in it.
> " | grep "has";
(2) has

Well looks like a return literal does not affect pipping. And we need a return before the closing quote to act as a RETURN key to send the echo statement for execution.

Kent Pawar
  • 1,276
  • 4
  • 16
  • 37
  • My comment was in regards to stuffing to screen. You don't do that here, and so the experiment, though a fine experiment, doesn't answer the question in regards to 'stuffing screen'. You can edit this answer though, so that is good. – vgoff Nov 18 '12 at 12:05