I am trying to execute, from a python3 script, Unix commands that looks like that:
mycommand `cmd_giving_a_path`/file
So I am using the subprocess.Popen function. But whatever I tried, the subprocess doesn’t evaluate the command between backquotes.
Here is what I tried:
>>> subprocess.Popen(['echo', 'toto'])
<subprocess.Popen object at 0x2a98df1c50>
>>> toto
Fine!
subprocess.Popen(['echo', 'toto', '`ls`'])
<subprocess.Popen object at 0x2a98df1d50>
>>> toto `ls`
Fine too!
>>> subprocess.Popen(['echo', 'toto', '`ls`'], shell=True)
<subprocess.Popen object at 0x2a98df1d90>
I don’t understand this one: I have no output at all.
With this command I expected that a shell is spawned and receive the command:
echo toto `ls`
But obsviously, I am wrong!
Could someone help with that?
Thank you!
PS: For those who may ask ‘why do you need this?’ the answer is:
because the options for the commands are read from an input file that may contains this kind of syntax.
From the pydoc documentation for subprocess: On UNIX, with shell=True: If args is a string, it specifies the command string to execute through the shell. If args is a sequence, the first item specifies the command string, and any additional items will be treated as additional shell arguments.
I.e., you were telling subprocess to run the following command:
As ephemient pointed out, what you really want to run is:
which you can do with either of: