我发誓我以前见过这个,但现在找不到了。是否可以让 shell 脚本启动 python interpeter“中流”,即:
#!/bin/bash
#shell stuff..
set +e
VAR=aabb
for i in a b c; do
echo $i
done
# same file!
#!/usr/bin/env python
# python would be given this fd which has been seek'd to this point
import sys
print ("xyzzy")
sys.exit(0)
最佳答案
您可以使用此 shell 语法(在 Unix 文献中称为 here document):
#!/bin/sh
echo this is a shell script
python <<@@
print 'hello from Python!'
@@
'<<' 运算符之后的标记可以是任意标识符,人们经常使用 EOF(文件结尾)或 EOD(文档结尾)之类的东西。如果标记开始一行,则 shell 将其解释为程序输入的结尾。
关于python - 可以混合使用 shell 和 python 脚本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9575715/