Shell – How to write a shell script to run multiple command in different screen sessions

gnu-screenshell-scriptzsh

My goal is to write a zsh script in order to create multiple screen sessions and run separate commands on each of them. I would also like to check at the beginning of my script if there are any screens with running jobs and terminate all of them that sit idle. Furthermore, if possible I would like to have a way to record the stdout of the individual screen and get their ID number. So far I have tried the following:

### Script for running everything in screens ###
killall -15 screen ## We make sure that no screens are running for now
#bkg_array = ("TopJets" "BosonJets" "DiBoson" "TTbar")

screen -dmS "MYSCREEN"
screen -S "MYSCREEN" -p 0 -X stuff 'echo "The array has of elements."\n'

However, I cannot seem to send the keystroke for pressing Enter; how can I achieve that effect? Additionally without the -p option the command is not sent to the screen. After pressing Ctrl+V+Enter the characters ^M were pasted on the screen and it pressed enter in the screen session, but I have no idea why that worked.

Thanks for your help, feel free to point out any ambiguities.

Best Answer

I find that while screen doesn't interpret named characters such as \n, it does take care of octal escapes. So instead of stuff 'echo "The array has of elements."\n', you could use:

stuff 'echo "The array has of elements."'\012
Related Question