0

I have to run a bash file through python for a Django webapp project but I cannot retrieve the output of the bash file (which I really need since it needs to be displayed on a web page). here is the code:

in my python file:

import subprocess 


output = subprocess.Popen(['bash', 'countingbash.sh', "hello", "world"])
print (output)

in countingbash.sh:

#!/bin/sh
file="$@"
for f in $file
    do
    echo "Argument is $f"

done

I need to capture the output since it needs to be displayed later on in a webpage. If I run the .py file, the output I get is not what the output should be by running a bash file. The output looks like this:

<subprocess.Popen object at 0x102cab290>

whereas it should be:

Argument is hello
Argument is world

If I run the file alone without writing print(output), the editor shows the output but how do i capture it?

1 Answer 1

1

This modification should give you correct output , I have opened stdout pipe and converted the shell output from binary string.I have used absolute path to avoid any of the working directory problem as well :

output = subprocess.Popen(['bash', '/home/demo/test.sh', "hello", "world"], stdout=subprocess.PIPE)
print (output.communicate()[0].decode('ascii'))
Sign up to request clarification or add additional context in comments.

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.