python subprocess calling bash script - need to print the quotes out too -


i'm having problem subprocess , printing quotes.

my python script takes user input, mashes around bit - , need send it's results bash script in manner.

myscript.sh 'var1 == var2 == b; othervar == c' /path/to/other/files 

where i'm getting hung on single quotes. python tries rip them out.

i used test.

subprocess.popen([myscript.sh 'var=11; ignore all' /path/to/files], shell=true, executable="/bin/bash") 

which returns invalid syntax pointing @ 2nd single quote. i've tried above without brackets , using single quotes outside , double quotes inside, etc.

other - would-like.

as saying above 'var == var == b; othervar == c' derived python script (in string format) - , i'll need call in subprocess this.

subprocess.popen([myscript.sh myvariables /path/to/files], shell=true, executable="/bin/bash") 

i have put single quotes around value of myvariables first example.

any pointers i'm going off correct method?

thank you.

when shell=true passed popen, pass whatever send on command line. means list should have 1 element. example:

subprocess.popen(['myscript.sh "var=11; ignore all" /path/to/files'], shell=true, executable="/bin/bash") 

or if /path/to/files variable in python environment:

subprocess.popen(['myscript.sh "var=11; ignore all" %s' % path_to_files], shell=true, executable="/bin/bash") 

having said strongly encourage not use shell argument. reason fragility. you'll more robust way of doing this:

subprocess.popen(["/bin/bash", "myscript.sh", "var=11; ignore all", path_to_files]) 

note "var=11; ignore all" passed 1 argument script. if separate arguments, make them separate list elements.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -