如何解决如何传递在python脚本中创建的变量以用于bash脚本?
我正在使用Python 3.6.3。
我创建了一个python脚本'create.py'。该python脚本调用并运行bash脚本“ verify.sh”。
“ verify.sh”脚本发送电子邮件:
#!/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}
因此在我的python脚本中确定了“ x”。我想将x插入上面的'verify.sh'脚本中,以便它与电子邮件一起消失。
解决方法
这是一个示例。
首先是Python脚本:
#!/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])
第二个Bash脚本:
#!/bin/bash
recipient="$1"
subject="$2"
body="$3"
dummy_mailer -r "$recipient" -s "$subject" -b "$body"
可以使用ARG_MAX从Python脚本发送任意数量的args(请参阅subprocess.run()),只需将它们添加到给run()
的列表中即可。例如
subprocess.run([path_to_script,arg_1])
subprocess.run([path_to_script,arg_1,arg_2])
subprocess.run([path_to_script,arg_2,arg_3,arg_4])
,
如果这确实是您的verify.sh
脚本的全部范围,则可以将其完全删除。
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)
如果您使用的是3.7之前的Python版本,则需要使用universal_newlines=True
而不是text=True
。
您也不需要mail -s
;参见例如How to send an email with Python?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。