shell - 将 SCP 的标准输出发送到屏幕并将 STDERR 发送到文件

标签 shell stdout scp stderr

有很多关于使用 SCP 时如何重定向 out 和 err 的讨论,或者只是 stdout 本身,但我很好奇是否有人知道如何将 stderr 重定向到文件,同时 stdout 继续打印到屏幕上。

目前,如果我们像这样重定向 stderr:

scp user@XXXX:*.txt  . 2>errfile.txt

标准输出上不显示任何内容,仅在 errfile.txt 中捕获错误和回显。

最佳答案

这是它的工作原理

MPBAB:work anj$ cat test.sh
echo "Hello there"
echo "This is a test script"

badcommand
MPBAB:work anj$ ./test.sh 2> err.log
Hello there
This is a test script
MPBAB:work anj$ cat err.log
./test.sh: line 4: badcommand: command not found

如您所见,当 test.sh 尝试调用 badcommand 时,它会抛出错误。但是,两个 echo 语句运行良好,并显示在 STDOUT 中,并且 STDERR 记录到 err.log.

现在实现您尝试的方式 -

MPBAB:work anj$ ./test.sh . 2>err.log
Hello there
This is a test script
MPBAB:work anj$ cat err.log
./test.sh: line 4: badcommand: command not found

以同样的方式工作。在您的情况下,我被迫认为您发出的 scp 命令不正确。 scp 需要目的地。像这样的东西(我想 scp test.txt 到远程服务器)

MPBAB:work anj$ scp ./test.txt user@secure.server.com:/home/data 2>scperr.txt
user@secure.server.com's password: #<this is because I dont have the public key installed>
test.txt              100%  291     0.3KB/s   00:00    
MPBAB:work an$ cat scperr.txt
MPBAB:work anj$ 

所以你看到,文件被复制,并且在 STDOUT 中显示 test.txt 100% 291 0.3KB/s 00:00 并且因为没有错误 scperr.txt 为空。

关于shell - 将 SCP 的标准输出发送到屏幕并将 STDERR 发送到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11619165/

相关文章:

shell - 遍历文本文件并查找不在所有文件中的行

c - 从 stdout 读取并得到一个字符串

shell - 如何编写shell脚本使用cron自动重启tomcat7?

python - 包装子进程的标准输出/标准错误

stdout - 如何使任何 shell 命令的输出无缓冲?

要求输入密码的 Python 子进程

linux - 如何通过限制 bash shell 脚本中的线程数来一次复制五个文件而不是一个文件?

linux - 使用SCP后"no such file or directory"

linux - 从文件中提取版本

regex - shell 脚本。如何使用正则表达式提取字符串