grep 包括来自 rsync 输出的错误结果

grep includes wrong results from rsync output

我有一个脚本可以使用 rsync 备份一些文件。 因为我不想要完整的输出细节,所以我只使用 grep 到 select 我想看到的摘要输出:

rsync -PaSh --stats --delete -e 'ssh -p XXXX -i key' /source/ user@X.X.X.X:/destination/ 2>> output.txt |grep -e 'Number of regular files' -e 'Total transferred' -e 'bytes/sec'>> output.txt


这个 returns 一个很好的总结,例如:

Number of regular files transferred: 73
Total transferred file size: 165.68M bytes
sent 3.79M bytes  received 207.71K bytes  103.89K bytes/sec


备份位置之一是 Firefox 配置文件,其中的文件会随着浏览而频繁更改。
这意味着当无法再找到文件时 rsync 会抛出错误:file has vanished.

我想排除这个错误,但由于某种原因,错误消息一直显示,即使它不包含在 grep:

file has vanished: "file1"
file has vanished: "file2"
file has vanished: "file3"
rsync warning: some files vanished before they could be transferred (code 24) at main.c(1207) [sender=3.1.3]
Number of regular files transferred: 73
Total transferred file size: 165.68M bytes
sent 3.79M bytes  received 207.71K bytes  103.89K bytes/sec


有人可以帮忙吗?

谢谢

管道 | 将标准输出 stdout 重定向到其他进程的标准输入 stdin

然而,与标准输出并行的还有标准错误 stderr 流。 stdout 在文件描述符 1 上,stderr 在文件描述符 2 上。

要将文件描述符 2 重定向到文件描述符 1,请执行:

 rsync .... 2>&1 | grep ...

可以在网上的各个地方找到更多信息,可能相关文档在 posix shell redirection:

[...] These numbers are called "file descriptors". The values 0, 1, and 2 have special meaning and conventional uses and are implied by certain redirection operations; they are referred to as standard input, standard output, and standard error, respectively. Programs usually take their input from standard input, and write output on standard output. Error messages are usually written on standard error. [...]