用所需的字符串从python调用bash脚本

问题描述 投票:-1回答:1

我有一个python脚本,它从/ etc / shadow获取我正在执行的项目的用户密码。假设我在python脚本中获得的密码为“ jumbo”。那么,如何在python脚本中调用bash脚本呢?它必须采用将密码“ jumbo”传递给bash脚本才能使用的方式。

之所以将其传递给bash脚本,是因为我随后需要在bash脚本中(显然不是在命令行中)使用该密码登录用户(例如“ worker”),才能自动登录当bash脚本执行完毕并返回到python脚本以继续执行bash脚本调用之后的其余python代码时,将其添加到“工作者”帐户中。

编辑:我很困惑如何在python中调用bash脚本,也很困惑如何在bash脚本中“捕获”字符串。

python bash file shadow
1个回答
0
投票

如果要使用参数调用脚本,则可以使用check_call函数:

from subprocess import check_call

rc = check_call("./file.sh %s %s %s" % (agr1, str(arg2), arg3), shell=True)

remember使用str(arg)在传递之前将args转换为字符串。

© www.soinside.com 2019 - 2024. All rights reserved.