我正在使用以下命令删除文件夹中的四个最大尺寸文件:
find "/var/www/site1/" -maxdepth 1 -type f | xargs ls -1S | head -n 4 | xargs -d '\n' rm -f
它工作正常,但有时会抛出损坏的管道错误:
xargs: ls: terminated by signal 13
答案 0 :(得分:26)
我遇到了类似的问题,发现这个帖子正在寻找答案:
信号13表示某些东西写入管道,不再读取任何内容(例如,请参阅http://people.cs.pitt.edu/~alanjawi/cs449/code/shell/UnixSignals.htm)。
这里的要点是,当下面的head命令已经获得了它想要的所有输入并关闭了它的输入管道时,由xargs执行的ls命令仍在写入输出。因此,忽略它是安全的,但它很难看。另请参阅https://superuser.com/questions/554855/how-can-i-fix-a-broken-pipe-error
中接受的答案答案 1 :(得分:5)
您故意使用head -n 4
终止您的程序,因为您在“调用者”完成之前终止了它,因此会创建损坏的管道。由于您需要这样做,因此您可以通过将错误重定向到丢弃它的/dev/null
来忽略错误:
find "/var/www/site1/" -maxdepth 1 -type f | xargs ls -1S | head -n 4
| xargs -d '\n' rm -f 2>/dev/null
答案 2 :(得分:0)
我得到了同样的错误,“信号13终止”,在不同情况下,其他答案在这里帮我解决了问题。我想扩展问题的本质:
corpy386 ~/gw/Release/5.1_v9/ClaimCenter $ find . -name '*.pcf' -not -name '*build*' | xargs grep -l ClaimSnapshotGeneralPanelSet | ( read f && echo $f && grep 'def=' $f )
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.auto.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
xargs: grep: terminated by signal 13
所以这是同样的错误,当我知道有很多文件与我正在寻找的东西相匹配时,我只得到一行输出。问题是xargs
产生多行输出,而read
在结束之前只消耗一行。 xargs
尝试将其余结果写入其中一个管道,但接收端已经退出并回家。因此,信号13:断管。
修复是通过循环消耗所有xargs
的输出 - 将read f && do_some_things
(仅读取一次)更改为while read f; do do_some_things; done
。
corpy386 ~/gw/Release/5.1_v9/ClaimCenter $ **find . -name '*.pcf' -not -name '*build*' | xargs grep -l ClaimSnapshotGeneralPanelSet | while read f; do echo $f; grep 'def=' $f; done**
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.auto.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.gl.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.Pr.pcf
def="ClaimSnapshotGeneralPRPanelSet(Claim, Snapshot)"
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.Trav.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.wc.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/build/idea/classes/web/pcf/claim/snapshot/default/ClaimSnapshotLossDetailsScreen.default.pcf
def="ClaimSnapshotGeneralPanelSet(Claim, SnapshotParam)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.auto.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.gl.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.Pr.pcf
def="ClaimSnapshotGeneralPRPanelSet(Claim, Snapshot)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.Trav.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotGeneralPanelSet.wc.pcf
def="AddressSnapshotInputSet(Snapshot.LossLocation, Snapshot)"
./modules/configuration/config/web/pcf/claim/snapshot/default/ClaimSnapshotLossDetailsScreen.default.pcf
def="ClaimSnapshotGeneralPanelSet(Claim, SnapshotParam)"
这与OP的脚本不完全相同 - 他们想要输入的一部分并且故意切断它,我想要整个流并且意外地将其切断 - 但shell语义也是如此。程序往往被编写为继续运行直到他们已经消耗了所有输入,而不是测试他们的收件人是否仍在听。