【问题标题】:Have grep display errors only只有 grep 显示错误
【发布时间】:2018-06-10 16:01:28
【问题描述】:

我正在使用 macOS Sierra,它具有 bash 版本 3.2.57(1)-release。我想使用shasum -c /files.txt > /out.txt 验证文件的校验和,其中files.txt 包含校验和,out.txt 是保存结果的位置。

我想要做的是以某种方式使用 grep 来过滤掉并只将错误保存到out.txt。这怎么可能? $? 可以派上用场吗?

谢谢


其他信息:

--BEGIN /file.txt CONTENTS--
865f34fe7ba81e1337ddbdfc511547d190367bbf3dad21aeb6da3eec621044f5  /samplefile.good
93ddaab2f672ff976756f50677646cafebabe93244f615e687db01399cf3fbc6  /samplefile.bad

--END /file.txt CONTENTS--


--BEGIN USER INTERACTION--

$ shasum -c file.txt
/samplefile.good: OK
/samplefile.bad: FAILED
shasum: WARNING: 1 computed checksum did NOT match

$ shasum -c file.txt 2> errors.txt
/samplefile.good: OK
/samplefile.bad: FAILED

$ cat errors.txt
shasum: WARNING: 1 computed checksum did NOT match

$ shasum -c file.txt 2>&1 > errors.txt
shasum: WARNING: 1 computed checksum did NOT match

$ cat errors.txt
/samplefile.good: OK
/samplefile.bad: FAILED

--END USER INTERACTION--

这就是我想要的:

$ cat errors.txt
/samplefile.bad: FAILED

$ cat errors.txt
/samplefile.bad: FAILED
shasum: WARNING: 1 computed checksum did NOT match

【问题讨论】:

  • 看起来并不完全符合我的要求,但我仍然可以试一试。我只想让grep 或类似的东西,只过滤掉shasum 输出的错误。
  • 如果错误写入stderr,那么2> out.txt 就足够了。 (/out.txt 可能会得到Permission denied)。

标签: bash macos grep macos-sierra errorlevel


【解决方案1】:

您不需要使用grep 来获取shasum 命令中的各种错误:

$ shasum -c file.txt 2> errors.txt
$ cat error.txt 
shasum: file.txt: no properly formatted SHA1 checksum lines found

但如果你真的想捕捉一些错误,你确实可以像这样使用grep

$ shasum -c file.txt 2>&1 | grep -i "no properly formatted" > errors.txt

【讨论】:

  • shasum -c file.txt 2>&1 | ...。如果你想要only 错误,2>&1 >/dev/null.
  • @PesaThe +1 我的错! (我已经更新了我的答案,谢谢)
  • 谢谢,但我没有将正确的错误保存到errors.txtSee this pastebin paste for more info,因为 SE cmets 从来没有很好地格式化。
  • @leetbacoon 编辑您的问题并包含所需的输出。
  • @PesaThe 编辑了问题。
猜你喜欢
  • 2019-05-11
  • 2018-12-16
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
  • 2015-05-30
相关资源
最近更新 更多