0

I have a text file that contains 100 bash commands and i want to execute every 5 in a new screen , but I don't know if I can use "for" in this case .

For example we have those commands :-

mkdir 1
cd 1
rm -rf 1.txt
rm -rf 2.txt
cd ..
mkdir2
...

I want to create a new screen and send the first 5 commands to it to be executed and then send the other 5 to another screen to the end of the file .

How I can do that ? any help please with the coding ?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
abualameer94
  • 91
  • 3
  • 13
  • what? you want to spawn a new screen every 5 commands? why are you doing that? why dont you just redirect the output to a file or something? – Joran Beasley Mar 25 '14 at 23:46
  • the commands that i am sending will take a long time as it is dealing with a huge files . I just wrote this as an example . The python script is just automate the process for the whole commands – abualameer94 Mar 25 '14 at 23:51
  • What have you tried? Python has a [`subprocess`](http://docs.python.org/2/library/subprocess.html) module. – ChrisP Mar 25 '14 at 23:59
  • I used `os.system("scren -m -d {0}".format(line))` but this sends every line in the text to a new screen , I want to send each 5 togather – abualameer94 Mar 26 '14 at 00:12

1 Answers1

1

I don't see any point why to do this, but you can use your solution in combination with buffering 5 lines from input and joining them by ;:

with open('commands.sh') as file:
    buffer = []
    for i, command in enumerate(file):
        buffer.append(command)
        if i % 5 == 0:
            os.system('screen -m -d "{0}"'.format(';'.join(buffer)))
            buffer = []
tomasbedrich
  • 1,350
  • 18
  • 26