在bash中将参数传递给嵌入式python脚本

时间:2015-06-25 21:29:54

标签: python bash

我在将参数传递给嵌入式bash脚本时遇到了困难。

<!DOCTYPE html>
<meta charset="UTF-8">
<title>Three column layout</title>
<style>
body { margin: 0; overflow: hidden }
#X { position: absolute; height: 200px; overflow: auto; padding: 9px; border: 1px solid red; margin: 9px 210px 0 9px; left: 0; right: 50% }
#Y { position: absolute; height: 200px; overflow: auto; padding: 9px; border: 1px solid red; margin: 9px -200px 0 -200px; left: 50%; right: 50% }
#Z { position: absolute; height: 200px; overflow: auto; padding: 9px; border: 1px solid red; margin: 9px 9px 0 210px; left: 50%; right: 0 }
</style>
<div id=X>
Left content
</div>
<div id=Y>
Middle content
</div>
<div id=Z>
Right content
</div>

我已经阅读了Steve's Blog: Embedding python in bash scripts这有帮助,但我还是无法通过论证。我已经尝试过解析器和myparser作为环境变量。

是否像定义$ 2一样简单并单独传递它们?

由于

1 个答案:

答案 0 :(得分:2)

你过分复杂了。为什么搞乱解析器

value="hello" python -c 'import os; print os.environ["value"]'

或者,对于更长的剧本:

value="hello" python <<'EOF'
import os
print os.environ["value"]
EOF

如果您需要设置sys.argv以与现有代码兼容:

python - first second <<<'import sys; print sys.argv'

因此:

args=( -s /mnt/folder -r /storagefolder/ )
python - "${args[@]}" <<'EOF'
import sys
print sys.argv # this is what an OptionParser will be looking at by default.
EOF
相关问题