重定向与模式匹配的SCP输出

发布于 2025-02-12 11:26:40 字数 350 浏览 2 评论 0 原文

我希望将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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2025-02-19 11:26:41

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 ,因此您可以

source my_scp_script.sh 2>&1 | grep "Permission Denied" > output.log

将文件描述符 2 重定向到文件描述符 1 1 piping之前 stdout to grep

使用 |&amp; 2&gt; 1 | 的快捷方式,您最终可以将其简化为:

source my_scp_script.sh |& grep "Permission Denied" > output.log

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 descriptors 1 (aka stdout; note that 1> can also be written as >, which is more commonly used) and 2 (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 with grep's stdin,
but as you need to connect stderr, you can use

source my_scp_script.sh 2>&1 | grep "Permission Denied" > output.log

to redirect file descriptor 2 to file descriptor 1 before piping the combined stdout to grep.

With |& being a shortcut for 2>&1 |, you can finally simplify this to:

source my_scp_script.sh |& grep "Permission Denied" > output.log
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文