Issue
I am converting a bash
script into python
script. In the bash script there is one section that basically sends mail like
( echo "some_body"
echo "some header"
) | ./sendmail [email protected]
I am trying to convert it like this
from subprocess import Popen, PIPE, STDOUT
p = Popen(['./sendmail', '[email protected]'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
stdout_data = p.communicate(input='data_to_write')[0]
But I am not sure how to pass multiple inputs like in the multiple echo
shown in the bash script.
Additional Question:
Apart from this, I have one more question, in the bash scripts there is one command always on top, like
. /opt/../some_necessary_exports.sh
.
.
. other stuff
here some_necessary_exports.sh
is like
export SOMETHING="some_thing"
export SOME_OTHER_THING="some_other_thing"
.
.
.
In the python script, I am calling Linux commands using the subprocess module like--
p1 = Popen(['bash', 'some_necessary_exports.sh'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
p2 = Popen(['bash', 'other_command'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
p3 = Popen(['bash', 'other_command'], stdout=PIPE, stdin=PIPE, stderr=PIPE)
I am not sure if these exports will be persisted in other commands. What will be the best way to handle this?
Solution
Simply concatenate the strings you want to send with newline between them.
stdout_data = p.communicate(input=some_header + "\n" + some_body)[0]
Answered By - Barmar Answer Checked By - Katrina (WPSolving Volunteer)