BashスクリプトにPythonスクリプトを含める方法

BashスクリプトにPythonスクリプトを含める方法

Bashスクリプトに次のPythonスクリプトを含める必要があります。

Bash スクリプトが正常に終了したら、次のスクリプトを実行する必要があります。

#!/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()

ベストアンサー1

HereDocをpython -

Pythonのヘルプから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"

おすすめ記事