有很多关于使用 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/