将bash变量传递给python脚本
发布时间:2020-12-16 01:23:39 所属栏目:安全 来源:网络整理
导读:将bash变量传递给 python脚本的最佳方式是什么?我想做一些像下面这样的事情: $cat test.sh#!/bin/bashfoo="hi"python -c 'import test; test.printfoo($foo)'$cat test.py#!/bin/pythondef printfoo(str): print str 当我尝试运行bash脚本时,我会收到语法
将bash变量传递给
python脚本的最佳方式是什么?我想做一些像下面这样的事情:
$cat test.sh #!/bin/bash foo="hi" python -c 'import test; test.printfoo($foo)' $cat test.py #!/bin/python def printfoo(str): print str 当我尝试运行bash脚本时,我会收到语法错误: File "<string>",line 1 import test; test.printfoo($foo) ^ SyntaxError: invalid syntax
总之,这样做:
... python -c "import test; test.printfoo('$foo')" ... 更新: 如果您认为字符串可能包含单引号(‘),如下面的注释中的@Gordon所述,您可以在bash中轻松地转义这些单引号.这是另一种解决方案: ... python -c "import test; test.printfoo('"${foo//'/'}"');" ... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |