我在Python脚本中运行bash脚本,需要使用在bash脚本中定义的变量。你知道吗
对于某些上下文,下面是bash脚本:
#!/bin/bash
updates=$(/usr/lib/update-notifier/apt-check 2>&1)
all=${updates%";"*}
security=${updates#*";"}
下面是我在Python脚本中如何调用它:
import subprocess
subprocess.call(["/usr/bin/checkupdates"])
我想在这个SQL update语句(Python脚本的一部分)中使用bash脚本(“all”和“security”)中定义的变量:
cursor.execute("update dbo.updates_preprod set updates_available = ?, securityupdates_available = ? where hostname = ?",(all, security , socket.gethostname()))
cnxn.commit()
有可能这样做吗?如果不是,我可以运行两个单独的脚本(每个脚本将回显其中一个变量)并获取每个脚本的stdout并在Python中将其定义为一个变量吗?你知道吗
提前谢谢!你知道吗
最后,直接从Python脚本调用该命令要容易得多。你知道吗
output = subprocess.check_output("usr/lib/update-notifier/apt-check", shell=True) all,sec = output.decode().split(';')
感谢@cdarke和@furas的建议。你知道吗
相关问题 更多 >
编程相关推荐