我有一个 python 文件,我想获取输出,就像我在交互模式下输入内容一样(就像这个问题https://stackoverflow.com/q/59008423但使用 shell 脚本)
我尝试使用这个 shell 脚本script.sh
# script.sh
#!/bin/sh
printf "print(\"one\")\nprint(\"two\")\n" | tee /dev/fd/3 | python -i 1>/dev/fd/3 2>/dev/fd/3
并运行
$ sh script.sh 3>&1
并得到输出
print("one")
print("two")
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> one
>>> two
>>>
而不是我所期望的
Python 3.11.6 (main, Nov 14 2023, 09:36:21) [GCC 13.2.1 20230801] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print("one")
one
>>> print("two")
two
>>>
我认为问题在于python
命令有一个标准输入缓冲区。