ckn*_*oll 7 python bash multiline
我需要扩展一个shell脚本(bash).由于我对python更熟悉,我想通过编写一些python代码来实现这一点,这些代码依赖于shell脚本中的变量.添加额外的python文件不是一种选择.
result=`python -c "import stuff; print('all $code in one very long line')"`
Run Code Online (Sandbox Code Playgroud)
不太可读.
我更喜欢将我的python代码指定为多行字符串,然后执行它.
Bar*_*mar 12
使用here-doc:
result=$(python <<EOF
import stuff
print('all $code in one very long line')
EOF
)
Run Code Online (Sandbox Code Playgroud)
Tanks to this SO answer我自己找到了答案:
#!/bin/bash
# some bash code
END_VALUE=10
PYTHON_CODE=$(cat <<END
# python code starts here
import math
for i in range($END_VALUE):
print(i, math.sqrt(i))
# python code ends here
END
)
# use the
res="$(python3 -c "$PYTHON_CODE")"
# continue with bash code
echo "$res"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3005 次 |
| 最近记录: |