Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash脚本到pythonscript的转换_Python_Bash_Git_Python 2.7_Shell - Fatal编程技术网

Bash脚本到pythonscript的转换

Bash脚本到pythonscript的转换,python,bash,git,python-2.7,shell,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(Version2.7)。在执行python代码时,我得到了一条消息repo-command-not-found PYTHON代码: os.s

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(Version2.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(),得到了相同的错误消息。

您至少可以使用repo命令的完整路径


这将避免子shell必须知道repo命令的位置(意味着在每个
os.system
调用上设置正确的
路径)。

此调用中存在问题:

os.system("source /proj/common/tools/repo/etc/profile.d/repo.sh")
问题在于,它在一个单独的子shell中运行
source
,当子shell退出时,对环境的所有更改(
cd
命令,如果有任何和环境变量,最显著的
路径
)都消失了


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

每个
os.system
调用调用一个唯一的shell。
os.system(“source…”
命令对与
os.system(“repo…
。通常,除非您重新实现
repo.sh
,否则您在这里不会有好运。除了子shell问题,这有什么意义?如果这是一个练习,恐怕您误解了。转换为Python脚本将涉及对原始源代码的
os.system
的零调用,除非这是非常必要的。正如中所述,Python中不存在这样做的库,编写一个库的成本会高得让人望而却步。我尝试了subprocess.subprocess.call([“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],shell=True)。当我在python交互模式下执行同一命令时,它显示相同的错误消息,有时它工作。有时它不工作。我不知道为什么。这取决于启动python之前发送的
PATH
的值。当我在bash中执行“source poky/fnc init build env build”时,poky build中的目录将更改(.即/数据/用户/依赖项/Lseries/poky/build)。但当我在python中执行时,目录并没有进入poky/build。我能知道如何在常规shell中使用它吗?do
which repo
:这将为您提供完整的使用路径。有没有办法使用python执行此功能code@sabarishs用python做的一个
等价物是:现在我得到了一个错误,when我执行“os.system”(“bitbake-g fss-image-full-l100”)。因为目录不在“poky/build”内。请帮助我解决此问题