简体   繁体   English

使用 Bash 重定向管道中的错误时遇到问题?

[英]Trouble redirecting an error in pipeline using Bash?

ls -lhR /etc/ | egrep *.conf$ >/home/student/total_size.txt 2>/home/student/error.txt

So I used this command to get all.conf files from /etc/ .所以我使用这个命令从/etc/获取 all.conf 文件。 I want the output in total_size.txt and my errors in error.txt .我想要total_size.txt中的error.txt和 error.txt 中的错误。 My output looks good, but the errors won't redirect to error.txt , they appear in my terminal:我的 output 看起来不错,但错误不会重定向到error.txt ,它们出现在我的终端中:

ls: cannot open directory '/etc/cups/ssl': Permission denied

I don't know what to do;我不知道该怎么办; I tried 2>> instead of 2> but it won't work either.我尝试了2>>而不是2>但它也不起作用。

This happens because ls 's stderr still points to the terminal.发生这种情况是因为lsstderr仍然指向终端。 You need to wrap pipeline in curly braces, and do the redirection outside.您需要将管道包裹在花括号中,并在外部进行重定向。 Eg:例如:

{ ls -lhR /etc/ | egrep *.conf$; } >/home/student/total_size.txt 2>/home/student/error.txt

Try this, should do the trick.试试这个,应该可以解决问题。

ls -lhR /etc/ 2>>/home/student/error.txt | egrep *.conf$ >/home/student/total_size.txt

The errors are generated by ls , not egrep .错误是由ls生成的,而不是egrep

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM