python - 可以混合使用 shell 和 python 脚本吗?

标签 python linux shell

我发誓我以前见过这个,但现在找不到了。是否可以让 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/

相关文章:

python - Raspberry Pi 3B 上的 pymodbus 和电表连接错误

python - 有没有办法将单个索引 pandas DataFrame reshape 为多索引以适应时间序列?

java - 为什么android不希望我proc mounts

linux - 流水线化 stderr 时如何使行缓冲工作?

bash - Unix shell 之间的可移植性——我对这个问题的思考是否正确?

Python pandas 条件列处理 None

python - 如何获取 pandas 中附加/合并 DataFrame 的行数?

c++ - 如何检查应用程序是否在 OpenOnload 下运行?

linux - 有没有一个linux命令可以读取一定数量的字符,包括控制字符?

shell - 使用 sed 打印匹配行