0

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?

1 Answer 1

1

Simply concatenate the strings you want to send with newline between them.

stdout_data = p.communicate(input=some_header + "\n" + some_body)[0]
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, can you provide some suggestions for the second question?

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.