user3803714
user3803714

Reputation: 5389

Passing parameters to shell script from a python program

I would like to call a shell script from my python script. I need to pass 3 parameters/arguments to the shell script. I am able to call the shell script (that is in the same directory as that of the python script), but having some issue with the parameter passing

from subprocess import call

// other code here.
line = "Hello"
// Here is how I call the shell command
call (["./myscript.sh", "/usr/share/file1.txt", ""/usr/share/file2.txt", line], shell=True)

In my shell script I have this
#!/bin/sh

echo "Parameters are $1 $2 $3"
...

Unfortunately parameters are not getting passed correctly.

I get this message:

Parameters are 

None of the parameter values are passed in the script

Upvotes: 1

Views: 3672

Answers (2)

jfs
jfs

Reputation: 414745

Drop shell=True (you might need to make myscript.sh executable: $ chmod +x myscript.sh):

#!/usr/bin/env python
from subprocess import check_call

line = "Hello world!"
check_call(["./myscript.sh", "/usr/share/file1.txt", "/usr/share/file2.txt", 
            line])

Do not use a list argument and shell=True together.

Upvotes: 0

vks
vks

Reputation: 67988

call ("./myscript.sh /usr/share/file1.txt /usr/share/file2.txt "+line, shell=True)

When you are using shell=True you can directly pass the command as if passing on shell directly.

Upvotes: 2

Related Questions