Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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 <[email protected]>"
to_addr = "<[email protected]"
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()
share|improve this question
2  
script.sh && python script.py ? –  Costas Feb 13 at 18:04
1  
Why "include" it? Why not just run it? –  terdon Feb 13 at 18:10
    
To call bash-script from python import os os.system ("./script.sh") –  Costas Feb 13 at 18:17

3 Answers 3

up vote 1 down vote accepted

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
Executing a bash statement
Hello python
100
share|improve this answer
    
Per OP's comment to another answer, I updated my answer which takes care of bash variables in the python script. –  Ketan Feb 13 at 18:32
    
I can't get the variable's value from bash-script into python script. –  Amal P Ramesh Mar 9 at 10:29
    
I replace subprocess.call(["echo","\$bashvar"]) into subprocess.call(["echo","bashvar"]) now it's working. –  Amal P Ramesh Mar 9 at 10:35

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"
share|improve this answer

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
share|improve this answer
    
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 at 18:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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