我希望将SCP的输出重定向到文件,但只有与模式匹配的行。例如,我得到了很多许可,拒绝了错误,因此我想记录所有情况的所有文件。我发现我可以将SCP的所有输出重定向到这样的文件:
source my_scp_script.sh > output.log 2>&1
脚本只是对SCP的简单调用。我遇到了如何匹配诸如“权限拒绝”之类的模式,而仅将这些行写入文件,而不是所有内容,因为有成千上万的文件成功。
编辑:我忘了澄清一下我已经尝试使用GREP,但是这样做时它不起作用 source my_scp_script.sh | GREP许可> output.log
I want redirect output from scp into a file, but only lines that match a pattern. For example, I'm getting a lot of permission denied errors, so I want to log all the files where that is the case. I found that I can redirect all output of scp into a file like this:
source my_scp_script.sh > output.log 2>&1
The script is just a simple call to scp. I'm stuck on how I can match a pattern like "Permission Denied" and only write those lines to the file, not everything since there are thousands of files that are successful.
EDIT: I forgot to clarify that I have tried using grep, but it does not work when doing it like this source my_scp_script.sh | grep Permission > output.log
发布评论
评论(1)
scp
没有什么特别的;您只需要熟悉shell i/o linux文档项目的 高级bash-scripting指南 IMO是一个很好的信息资源; href =“ https://tldp.org/ldp/abs/html/io-redirection.html” rel =“ nofollow noreferrer”>§20i/o重定向包含一个不错的摘要。)您可以使用<<<<< )代码>源my_scp_script.sh 1&gt; /tmp/stdout 2&gt; /tmp/stderr 将写入的输出重定向到文件描述符
1
(aka stdout ;请注意,1&gt;
也可以是写为&gt;
,更常用)和2
(aka stderr)到临时文件。然后,您可以检查/tmp/std {out,err}
,并会发现权限拒绝错误错误 stderr。一个简单< a href =“ https://www.gnu.org/software/bash/manual/html_node/pipelines.html” rel =“ nofollow noreferrer“>管道
| grep
将 stdout 与grep
's stdin ,但是,由于您需要连接 stderr ,因此您可以
将文件描述符
2
重定向到文件描述符1
1 piping之前 stdout togrep
。使用
|&amp;
是2&gt; 1 |
的快捷方式,您最终可以将其简化为:There is nothing special about
scp
; you just need to get familiar with shell I/O redirections.(The Linux Documentation Project's Advanced Bash-Scripting Guide is IMO a great resource of information; §20 I/O Redirection contains a nice summary.)
You could use
source my_scp_script.sh 1> /tmp/stdout 2> /tmp/stderr
to redirect the output written to the file descriptors1
(aka stdout; note that1>
can also be written as>
, which is more commonly used) and2
(aka stderr) to temporary files. You can then inspect/tmp/std{out,err}
and will find that the Permission Denied errors are written to stderr.A simple pipeline
| grep
connects stdout withgrep
's stdin,but as you need to connect stderr, you can use
to redirect file descriptor
2
to file descriptor1
before piping the combined stdout togrep
.With
|&
being a shortcut for2>&1 |
, you can finally simplify this to: