6

I have a script say abc.sh which has list of commands with flags. example

//abc.sh
echo $FLAG_name
cp   $FLAG_file1   $FLAG_file2
echo 'file copied'

I want to execute this script through python code. say

//xyz.py

name = 'FUnCOder'
filename1  = 'aaa.txt'
filename2 = 'bbb.txt'

subprocess.call([abc.sh, name, filename1, filname2], stdout=PIPE, stderr=PIPE, shell=True)

This call is not working.

What are the other options?

Also the shell script file is in some other directory. And I want the output to go in logs.

2
  • have you considered using shutls instead of a bash script. Try shutils.copyfile Commented Apr 23, 2013 at 23:17
  • 1
    Using shell=True here is wrong and bug-inducing. Commented Apr 24, 2013 at 15:27

4 Answers 4

5

Usually you want to use Popen since you have process control afterwards. Try:

import subprocess
process = subprocess.Popen(['abc.sh', name, filename1, filname2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process.wait() # Wait for process to complete.

# iterate on the stdout line by line
for line in process.stdout.readlines():
    print(line)
Sign up to request clarification or add additional context in comments.

12 Comments

I tried this but couldn't get any output on screen... Can you explain a little about redirecting to PIPE and not STDOUT?
By redirecting to PIPE you can read the stdout of your script by using process.stdout.readlines() or process.stdout.read() etc.
I think it's also the only alternative since he wants to log the output.
it worked but the script is not taking any arguments that I'm passing. what could be the problem?
If you are using the same script as the one on your original question, then you are doing it wrong. Shell scripts arguments are passed as $1 $2 $3 etc.
|
1

Try this:

//xyz.py

name = 'FUnCOder'
filename1  = 'aaa.txt'
filename2 = 'bbb.txt'

process = subprocess.Popen(['abc.sh', name, filename1, filname2], stdout=PIPE)
process.wait()

Notice that 'abc.sh' is in quotes because it's not a variable name, but the command you're calling.

I would also, in general, recommend using shell=False, though in some cases it is necessary to use shell=True.

To put output into a file try:

with open("logfile.log") as file:
    file.writelines(process.stdout)

5 Comments

Try running it without the PIPEs
i printed the retcode of this call and its says 0...which mean its failing... any idea why?
return code 0 means it's not failing, en.wikipedia.org/wiki/Exit_status#POSIX.
@RishabhAgarwal Don't trust everything you read on the web. The wikipedia reference is correct.
1

I know this is a old question, below is the way if you are using Python 3.5 & above versions.

import subprocess
process = subprocess.run('script.sh', shell=True, check=True, timeout=10) 

Ref: https://docs.python.org/3.5/library/subprocess.html#subprocess.run

Comments

0

I am on macOS and to run a shell script I used:

process = subprocess.Popen([abc.sh, name, filename1, filname2], shell=True)

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.