【问题标题】:bzip command not working with "tee -a"bzip 命令不适用于“tee -a”
【发布时间】:2015-10-23 08:23:21
【问题描述】:

我想使用 tee 命令将 bzip 命令的 stdop 重定向到日志文件,但它不起作用,并且在 tee 命令中出现“-a”错误。请参阅下面的错误,

> bzip2 file -c 1> tee -a logfile

bzip2: Bad flag `-a'
bzip2, a block-sorting file compressor.  Version 1.0.5, 10-Dec-2007.

   usage: bzip2 [flags and input files in any order]

   -h --help           print this message
   -d --decompress     force decompression
   -z --compress       force compression
   -k --keep           keep (don't delete) input files
   -f --force          overwrite existing output files
   -t --test           test compressed file integrity
   -c --stdout         output to standard out
   -q --quiet          suppress noncritical error messages
   -v --verbose        be verbose (a 2nd -v gives more)
   -L --license        display software version & license
   -V --version        display software version & license
   -s --small          use less memory (at most 2500k)
   -1 .. -9            set block size to 100k .. 900k
   --fast              alias for -1
   --best              alias for -9

   If invoked as `bzip2', default action is to compress.
              as `bunzip2',  default action is to decompress.
              as `bzcat', default action is to decompress to stdout.

   If no file names are given, bzip2 compresses or decompresses
   from standard input to standard output.  You can combine
   short flags, so `-v -4' means the same as -v4 or -4v, &c.

有什么问题?为什么 bzip 正在考虑 tee 命令的 '-a' 标志。

【问题讨论】:

    标签: linux bash shell unix bzip2


    【解决方案1】:

    试试:

    bzip2 -c file | tee -a logfile
    

    |(管道)将左侧命令的标准输出重定向到右侧命令的标准输入。

    -c 是 bzip2 的一个选项,上面写着Compress or decompress to standard output.。见man bzip2

    【讨论】:

    • 可能需要bzip2 -c 输出到标准输出(管道)。
    • 它不工作>bzip2 -c 文件 | tee -a 日志文件 BZh9rE8P 。输出为 BZh9rE8P 且未压缩文件。在实际场景中,我无法更改命令的 '1> tee -a logfile' 部分,原因是它在函数中使用(如 $@ 1> tee -a logfile )$@ 用于执行函数中的许多命令.
    • 如果你想就地压缩文件,你不能使用'-c',只需要bzip2 file | tee ...此外1>tee -a logfile是公然错误的,并且总是会导致-a logfgile给 bzip2。
    【解决方案2】:

    您的问题是1> 不会将bzip2 命令的输出通过管道传输到tee 命令,而是将输出重定向到名为tee 的文件。此外,您可能不想使用-c。您应该改用管道|,如下所示:

    bzip2 file | tee -a logfile
    

    另外,bzip2 抱怨的原因是因为你上面提到的命令将被完全解释为这个:

    bzip2 file -a logfile 1> tee
    

    因此tee之后的所有参数实际上都添加到bzip2命令中。

    【讨论】:

    • 感谢您的解释。命令bzip2 file -c | tee -a logfile 也不起作用。它打印一些垃圾行并且不创建压缩文件。在实际场景中,我无法更改上面命令的 '1> tee -a logfile' 部分,原因是它在函数中使用(如 $@ 1> tee -a logfile )$@ 用于执行许多命令在函数中
    • 我猜你不应该使用 -c ;相应地更新了帖子。此外,您应该更正此功能,因为它显然是错误的。
    【解决方案3】:

    正如其他人指出的那样,您需要一个管道,而不是输出重定向:

    bzip2 file | tee -a logfile
    

    但是,bzip2 不会产生任何输出;它只是用文件的压缩版本替换给定的文件。您可能希望将标准 error 传送到日志文件:

    bzip2 file 2>&1 | tee -a logfile
    

    2>&1 将标准错误复制到标准输出,然后可以通过管道传输。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-17
      • 2011-03-28
      • 1970-01-01
      • 2016-07-17
      • 2013-07-09
      • 1970-01-01
      • 2014-04-24
      相关资源
      最近更新 更多