3
有很多关于如何重定向使用SCP或者使用stdout的方法,但我很好奇如果有人知道如何将stderr重定向到一个文件,而标准输出继续被打印到屏幕上。发送SCP的stdout到屏幕和STDERR到文件
目前,如果我们重定向标准错误,像这样:
scp [email protected]:*.txt . 2>errfile.txt
没有显示在标准输出,只有错误和回声在errfile.txt被捕获。
有很多关于如何重定向使用SCP或者使用stdout的方法,但我很好奇如果有人知道如何将stderr重定向到一个文件,而标准输出继续被打印到屏幕上。发送SCP的stdout到屏幕和STDERR到文件
目前,如果我们重定向标准错误,像这样:
scp [email protected]:*.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
。
现在实施的方式,你tried-
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
需要source
和destination
。像这样的东西(在这里我想scp
的test.txt到远程服务器)
MPBAB:work anj$ scp ./test.txt [email protected]:/home/data 2>scperr.txt
[email protected]'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
是空的。
嗯,这正是你所做的。标准输出(进度信息)输出到您的终端,并将stderr发送到errfile.txt。顺便说一句,你最好从扩展的第一个参数保护明星:'\ *。txt'。否则,它会扩展到您所在计算机上的文件名,这不太可能是您想要的。 – fork0 2012-07-23 21:03:04
我试过这个:scp user @ XXX:/ $ PATH/test *。 2>/tmp/test,用openssh客户端,它的工作原理,我已经在sdout中复制了所有文件,并在“/ tmp/test”文件中拒绝了权限。 Sh - > dash和ssh客户端:OpenSSH_5.3p1 Debian-3ubuntu7,OpenSSL 0.9.8k 2009年3月25日 – Duffydake 2012-07-27 18:48:10