Bash – How to get the output inside `screen` out to a script

bashgnu-screenlogsrecording

I have a screen instance running, and I would need to execute some code inside the screen, and get the result out to my script.

The first part is quite easy, I just screen -S session_name -X eval 'stuff "$cmd"\015'.
(I modified a line I found in a script)

The second part, getting out the output, is trickier. How can I get the whole output, whatever it's size?

Best Answer

You could start screen with the -L option. This will cause screen to create a file screenlog.n (the n part is numerical, starting with a zero) in the current working directory.

In your case this would look something like: screen -S session_name -L -X eval 'stuff "$cmd"\015'

As long as you remember to clean up afterwards, this should match what you are after.

For last line of the log, it can easily be obtained with tail -1 screenlog.0, or the entire log can be parsed however you wish.

Related Question