使用所需的字符串从 python 调用 bash 脚本
Call a bash script from python with a desired string
我有一个 python 脚本,它从 /etc/shadow 获取我正在做的项目的用户密码。假设我在 python 脚本中得到的密码是“jumbo”。那么,我将如何在 python 脚本中调用 bash 脚本?它必须以密码“jumbo”传递给要使用的 bash 脚本的方式进行。
我将它传递给 bash 脚本的原因是因为我需要在 bash 脚本(而不是显然在命令行)在 bash 脚本完成执行时自动登录到“工人”帐户,并且它 returns 到 python 脚本以继续 python 代码在 bash 脚本调用之后。
编辑:我对如何在 python 中调用 bash 脚本感到困惑,也对如何在 bash 脚本中“捕获”字符串感到困惑。
如果你想调用带参数的脚本,你可以使用check_call
函数:
from subprocess import check_call
rc = check_call("./file.sh %s %s %s" % (agr1, str(arg2), arg3), shell=True)
记住 在传递之前使用 str(arg) 将 args 转换为字符串。
我有一个 python 脚本,它从 /etc/shadow 获取我正在做的项目的用户密码。假设我在 python 脚本中得到的密码是“jumbo”。那么,我将如何在 python 脚本中调用 bash 脚本?它必须以密码“jumbo”传递给要使用的 bash 脚本的方式进行。
我将它传递给 bash 脚本的原因是因为我需要在 bash 脚本(而不是显然在命令行)在 bash 脚本完成执行时自动登录到“工人”帐户,并且它 returns 到 python 脚本以继续 python 代码在 bash 脚本调用之后。
编辑:我对如何在 python 中调用 bash 脚本感到困惑,也对如何在 bash 脚本中“捕获”字符串感到困惑。
如果你想调用带参数的脚本,你可以使用check_call
函数:
from subprocess import check_call
rc = check_call("./file.sh %s %s %s" % (agr1, str(arg2), arg3), shell=True)
记住 在传递之前使用 str(arg) 将 args 转换为字符串。