shell里调用python代码及多行字符串变量
2022-12-19 本文已影响0人
九楼记
1.shell里调用python代码
场景是在shell里调用python代码
result=`python -c"import stuff; print('all $code in one very long line')"`
可读性差
result=$(python <<EOF
import stuff
print('all $code in one very long line')
EOF
)
#!/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"
2.shell变量内容多行字符串cat <<EOF
>输出到文件, >>追加到文件, <从文件读取输入。
>>是什么?
当在Bash中处理多行文本时, cat <<EOF语法非常有用。 将多行字符串分配给shell变量,文件或管道时。
这称为heredoc 格式,以将字符串提供给stdin。
3.Reference
https://www.liangzl.com/get-article-detail-162518.html
https://www.codenong.com/40143190/