python - Bash 脚本到 python 脚本的转换

标签 python bash git python-2.7 shell

BASH 代码:

source /proj/common/tools/repo/etc/profile.d/repo.sh
repo project init $branch
repo project sync
source poky/fnc-init-build-env build
bitbake -g $image

我将此 bash 代码转换为 python(版本 2.7)。在执行 python 代码时,我收到 repo command not found 消息。

Python 代码:

os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")
os.system("repo project init " + branch)
os.system("repo project sync")
os.system("source poky/fnc-init-build-env build")
os.chdir("poky/build")
os.system("bitbake -g " + image)

错误消息:

sh: repo: command not found
sh: repo: command not found

我尝试使用 subprocess.call(),但收到了相同的错误消息。

最佳答案

问题出在这个调用中:

os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")

问题是它在单独的子 shell 中运行 source ,并且当子 shell 退出时对环境的所有更改(如果有任何环境变量,则使用 cd 命令,大多数值得注意的PATH)消失了。

我的建议是继续使用您已经使用过的 shell 脚本 — 只需通过一次 os.system() 调用从 Python 中调用它即可。在 shell 脚本中,您可以使用 source

关于python - Bash 脚本到 python 脚本的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49993364/

相关文章:

python - Python 3.9 和 PEP 585 中的 Typing.Any - 标准集合中的类型提示泛型

bash - 在 Jenkins 中使用脚本在不同阶段运行

git - 如何使用cmd创建文件?

python - 使用模式重命名文件

python - 我应该使用什么函数将数据框的列四舍五入为数字?

linux - cp:找不到命令

git - 是否可以恢复在 Visual Studio -> Team Explorer -> Git 中所做的撤消更改?

Git 克隆除一个文件或文件夹之外的所有文件,可能吗?

python - 如何将数据框单元格中的关键字转换为每个自己的列

linux - 初学者的 Bash 脚本 : "Unary Operator Expected" error && how to use grep to search for a variable from the output of a command?