混合shell和python脚本可能吗?

use*_*904 6 python linux shell

我发誓我以前见过这个,但现在找不到它.是否有可能让shell脚本启动python interpeter"mid stream",即:

#!/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)
Run Code Online (Sandbox Code Playgroud)

pio*_*kuc 8

您可以使用此shell语法(在Unix文献中称为此文档):

#!/bin/sh
echo this is a shell script

python <<@@
print 'hello from Python!'
@@
Run Code Online (Sandbox Code Playgroud)

'<<'运算符之后的标记可以通过任意标识符,人们经常使用EOF(文件结束)或EOD(文档结尾)之类的东西.如果标记开始一行,则shell将其解释为程序的输入结束.