我的shell脚本中有以下代码:
bzip2 -dc $filename | head -10 > $output有时我会收到这个错误(启用调试输出):
+ head -10
+ bzip2 -dc mylog.bz2
bzip2: I/O or other error, bailing out. Possible reason follows.
bzip2: Broken pipe
Input file = mylog.bz2, output file = (stdout)看起来像是head命令突然退出,bzip2收到了SIGPIPE。我能用这个做什么?我需要确保无论发生什么,前10行都会在$output文件中。我想,如果其中一个进程不幸失败,就不能保证总是这样。
发布于 2016-07-01 16:18:18
当head命令在输出其行后退出时,bzip命令将失败。没有数据丢失;head命令已经完成了它的工作。
如果您担心这一点,您可以将对head的调用替换为执行相同操作的sed脚本:
bzip -dc "$filename" | sed -n '1,10p' >"$output"此sed脚本将读取管道中的所有数据,但在完成第10行操作后不会退出。
发布于 2016-07-01 16:13:58
看起来像是head命令突然退出,bzip2收到SIGPIPE。
你期望head做什么?它从输入中读取的内容与配置为输出的内容一样多,然后关闭。这在很大程度上是设计出来的。
另外:
头-10
我的head版本要求的内容更像是
head -n10发布于 2016-07-01 16:19:08
您可以使用xargs来避免从管道到head的空内容,这可能是SIGPIPE的原因。这样,即使bzip2没有提供任何输出,您也不会看到任何错误。
bzip2 -dc $filename | xargs -r head -10 > $output其中选项-r说
-r If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.https://stackoverflow.com/questions/38140309
复制相似问题