我正在尝试从基于 GUI 的软件启动几个 bash 例程。我面临的问题是管道问题。这里是测试 bash 脚本(bashScriptTest.sh):
#!/bin/bash
#---------- Working
ls | sort | grep d > testFile.txt
cat testFile.txt
#---------- NOT working
echo $RANDOM > testFile2.txt
for i in `seq 1 15000`; do
echo $RANDOM >> testFile2.txt
done
awk '{print $1}' testFile2.txt | sort -g | head -1
Run Code Online (Sandbox Code Playgroud)
这里是创建错误的python脚本:
import subprocess
#
with open('log.txt','w') as outfile:
CLEAN=subprocess.Popen("./bashScriptTest.sh", stdout=outfile, stderr=outfile)
print CLEAN.pid
OUTSEE=subprocess.Popen(['x-terminal-emulator', '-e','tail -f '+outfile.name])
Run Code Online (Sandbox Code Playgroud)
从运行 python 脚本可以看出,Broken-pipe 错误不是在前三个管道(第一行)中遇到的,而是在 awk 完成大量工作之后遇到的。我需要在 bash 中管理大量的例程和子例程,并且使用 shell==True 标志不会改变任何事情。我试图以最 Pythonic 的方式编写所有内容,但不幸的是,我没有机会重写 …