1

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
2
  • 1
    Works fine with shell=False, the recommended use -- why do you think you need shell=True instead?! Commented Feb 18, 2015 at 6:09
  • You don't want a list argument with shell=True anyway. Commented Feb 18, 2015 at 6:12

2 Answers 2

2
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.

Sign up to request clarification or add additional context in comments.

3 Comments

Yes, I tried doing that and it works fine except for the last argument. I see this in my script: Parameters are: /usr/share/file1.txt /usr/share/file2.txt line I would like to have: /usr/share/file1.txt /usr/share/file2.txt Hello
Yes, I did this: cmd = "./myscript.sh /usr/share/file1.txt /usr/share/file2.txt" + " " + line call([cmd],shell=True). That worked fine.
shell=True is unnecessary here. Also, it may break if line contains shell metacharacters such as $`!.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.