0

I am using Python 3.6.3.

I have created a python script 'create.py'. This python script calls and runs a bash script 'verify.sh'.

The 'verify.sh' script sends an email:

#!/bin/sh
emailGroup="dummy@email.com"
echo "The variable of interest is x(insert here): $1 " | mail -s "The variable of interest is x(insert here)" ${emailGroup}

So in my python script 'x' is determined. I want to insert x into the 'verify.sh' script above so that it goes out with the email.

Michael
  • 3
  • 4
  • 1
    The script accepts the variable of interest as its first command-line argument. To send `The variable of interest is x(insert here): fnord` you would call it like `verify.sh fnord`. You are not showing the relevant parts of your Python code but that's where you need to put it. (You don't really need a separate shell script for this; you could call `mail` directly from Python just as well, or use the `smtplib` library to send mail natively.) – tripleee Aug 24 '20 at 15:53

2 Answers2

0

Here's an example of how to do that.

First the Python script:

#!/usr/bin/python3
import subprocess
recipient = "me@stackoverflow.com"
subject = "Python Script Calls Bash Script"
body = "This is a test, yes it is, yes it is."
notify_script = "/home/me/Scripts/notify"
subprocess.run([notify_script, recipient, subject, body])

Second the Bash script:

#!/bin/bash
recipient="$1"
subject="$2"
body="$3"
dummy_mailer -r "$recipient" -s "$subject" -b "$body"

Any number of args (see ARG_MAX) can be sent from the Python script using subprocess.run(), simply add or remove them from the list given to run(). e.g.

subprocess.run([path_to_script, arg_1])
subprocess.run([path_to_script, arg_1, arg_2])
subprocess.run([path_to_script, arg_1, arg_2, arg_3, arg_4])
mattst
  • 13,340
  • 4
  • 31
  • 43
  • Ok so I have the 2 scripts running without errors, the only issue is that the email I receive from the bash script doesnt have the variable (created from the .py file) intact. i.e. I am receiving the equivalent of: "the value is and this is correct". As opposed to "the value is x and this is correct". It seems there its overlooking it. In the code I have the following: echo "the value is $1 and this is correct" – Michael Aug 24 '20 at 15:50
  • You should not advocate the use of `Popen` for tasks where the higher-level `subprocess` functions can take care of the rest of the necessary plumbing which you are not doing here. – tripleee Aug 24 '20 at 15:58
  • @tripleee I've changed it to use `subprocess.run()` - is that what you had in mind? – mattst Aug 25 '20 at 10:49
  • Yup, that's definitely an improvement, thanks. – tripleee Aug 25 '20 at 11:17
0

If that's really the full extent of your verify.sh script, you can remove it entirely.

import subprocess

sent = subprocess.run(
    ['mail', '-s', 'The variable of interest is x(insert here)', 'dummy@email.com'],
    input='The variable of interest is x(insert here): {0}'.format(x),
    text=True, check=True)

If you are on an earlier version of Python than 3.7, you will want to use universal_newlines=True instead of text=True.

You don't really need mail -s either; see e.g. How to send an email with Python?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • `import subprocess hdCount = subprocess.run('ls -Ap | egrep "^\..*/$" | wc -l', shell=True, universal_newlines=True, stdout=subprocess.PIPE); hd = subprocess.run('ls -Ap | egrep "^\..*/$"', shell=True, universal_newlines=True, stdout=subprocess.PIPE); if int(hdCount.stdout) != 0: sent = subprocess.run(['mail', '-s', 'The variable of interest is x(insert here)', 'dummy@email.com'], input='The variable of interest is): {0}'.format(hd), text=True, check=True) ` **I am getting an error of "TypeError: __init__() got an unexpected keyword argument 'text'" thank you **@tripleee – Michael Aug 25 '20 at 10:17
  • Sounds like you are on Python <= 3.6; the keyword argument used to be called `universal_newlines=True` somewhat misleadingly in older versions. – tripleee Aug 25 '20 at 10:24
  • But doing a `wc` on `grep` output is [another antipattern](http://www.iki.fi/era/unix/award.html#wc); I recognize this code and believe I have already pointed out a much better solution in a [separate answer.](https://stackoverflow.com/a/63525004/874188) Again, why are you using Python at all if you use the shell for almost everything? – tripleee Aug 25 '20 at 10:25