13

I need to include below python script inside a bash script.

If the bash script end success, I need to execute the below script:

#!/usr/bin/python    
from smtplib import SMTP
import datetime
debuglevel = 0

smtp = SMTP()
smtp.set_debuglevel(debuglevel)
smtp.connect('192.168.75.1', 25)
smtp.login('my_mail', 'mail_passwd')

from_addr = "My Name <my_mail@192.168.75.1>"
to_addr = "<my_mail@192.168.75.1"
subj = "Process completed"
date = datetime.datetime.now().strftime( "%d/%m/%Y %H:%M" )
#print (date)
message_text = "Hai..\n\nThe process completed."

msg = "From: %s\nTo: %s\nSubject: %s\nDate: %s\n\n%s" % ( from_addr, to_addr, subj, date, message_text )

smtp.sendmail(from_addr, to_addr, msg)
smtp.quit()
  • 4
    script.sh && python script.py ? – Costas Feb 13 '15 at 18:04
  • 2
    Why "include" it? Why not just run it? – terdon Feb 13 '15 at 18:10
  • 1
    To call bash-script from python import os os.system ("./script.sh") – Costas Feb 13 '15 at 18:17
8

You can use heredoc if you want to keep the source of both bash and python scripts together. For example, say the following are the contents of a file called pyinbash.sh:

#!/bin/bash

echo "Executing a bash statement"
export bashvar=100

cat << EOF > pyscript.py
#!/usr/bin/python
import subprocess

print 'Hello python'
subprocess.call(["echo","$bashvar"])

EOF

chmod 755 pyscript.py

./pyscript.py

Now running the pyinbash.sh will yield:

$ chmod 755 pyinbash.sh
$ ./pyinbash.sh
Exe
  • Per OP's comment to another answer, I updated my answer which takes care of bash variables in the python script. – mkc Feb 13 '15 at 18:32
  • 1
    I can't get the variable's value from bash-script into python script. – Amal P Ramesh Mar 9 '15 at 10:29
  • I replace subprocess.call(["echo","\$bashvar"]) into subprocess.call(["echo","bashvar"]) now it's working. – Amal P Ramesh Mar 9 '15 at 10:35
  • That was pretty neat! – SexyBeast Jan 13 '16 at 8:19
16

Just pass a HereDoc to python -.

From python help python -h:

- : program read from stdin

#!/bin/bash

MYSTRING="Do something in bash"
echo $MYSTRING

python - << EOF
myPyString = "Do something on python"
print myPyString

EOF

echo "Back to bash"
  • I don't see why this was downvoted. It's a simple and workable solution for some cases. It does have the (major) limitation that you can't use standard input in the python script, though (since it's receiving stdin from the heredoc). – pyrocrasty Feb 11 '17 at 9:01
  • I hate downvotes without comment. It works for me. Upvoted it ;-) – kev Mar 27 '18 at 3:47
  • Upvoted this answer because (unlike the accepted answer) it doesn't write the script to the file system. – Huw Walters Mar 28 at 9:16
2

The simplest approach is to just save the python script as, for example script.py and then either call it from the bash script, or call it after the bash script:

#!/usr/bin/env bash
echo "This is the bash script" &&
/path/to/script.py

Or

script.sh && script.py
  • On the bash-script execution time some variables are get defined, I need this on the python script for the message body. – Amal P Ramesh Feb 13 '15 at 18:23
0

How about this for an example:

PYTHON_BIN=/usr/bin/python
if [ -x $PYTHON_BIN ]; then
$PYTHON_BIN -c "print 'Hello, world'"
else
echo 'Hello, world'
fi

VS

$ ./foobar.py
env: python: No such file or directory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.