我有以下我想用Python编写的shell脚本(当然grep .
实际上是一个更复杂的命令):
#!/bin/bash (cat somefile 2>/dev/null || (echo 'somefile not found'; cat logfile)) \ | grep .
我尝试了这个(cat logfile
无论如何都缺少相同的东西):
#!/usr/bin/env python import StringIO import subprocess try: myfile = open('somefile') except: myfile = StringIO.StringIO('somefile not found') subprocess.call(['grep', '.'], stdin = myfile)
但是我得到了错误AttributeError: StringIO instance has no attribute 'fileno'
.
我知道我应该使用subprocess.communicate()
而不是StringIO来向grep
进程发送字符串,但我不知道如何混合字符串和文件.