I am trying to run a shell script called nn.sh (which constantly runs a Linux command over time), from within a python file. I am using the following piece of code:
from subprocess import call, Popen, PIPE
call(['/bin/sh', 'nn.sh', '172.20.125.44', '10', '>>', 'log.txt'])
This code is supposed to run nn.sh with inputs 172.20.125.44 and 10 and stores the result in the file log.txt. When I run this Python script, it only shows the results of running nn.sh on the screen and it does not save them in the fill log.txt. However, if I type
/bin/sh nn.sh 172.20.125.44 10 >> log.txt
in the command line, it is correctly saving all the data into the file log.txt. Any ideas on what goes wrong?
"out.txt; rm -rf *". If subprocess.call just dumped that as-is into a shell, you'd have a disaster on your hands. The correct solution is outlined by Anton Savin below. But, for your information, you could force subprocess.call to not escape anything by passing itshell = True, though this is a security issue and considered poor practice.