Bash,stdout 重定向命令,如 scp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3890809/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:45:25  来源:igfitidea点击:

Bash, stdout redirect of commands like scp

bashredirectstdout

提问by Daniele

I have a bash script with some scp commands inside. It works very well but, if I try to redirect my stdout with "./myscript.sh >log", only my explicit echos are shown in the "log" file. The scp output is missing.

我有一个 bash 脚本,里面有一些 scp 命令。它工作得很好,但是,如果我尝试使用“ ./myscript.sh >log”重定向我的标准输出,“日志”文件中只会显示我的显式回声。scp 输出丢失。

if $C_SFTP; then
   scp -r $C_SFTP_USER@$C_SFTP_HOST:$C_SOURCE "$C_TMPDIR"
fi

Ok, what should I do now? Thank you

好的,我现在该怎么办?谢谢

回答by Daniele

scpis using interactive terminal in order to print that fancy progress bar. Printing that output to a file does not make sense at all, so scpdetects when its output is redirected to somewhere else other than a terminal and does disable this output.

scp正在使用交互式终端来打印那个花哨的进度条。将该输出打印到文件根本没有意义,因此scp检测何时将其输出重定向到终端以外的其他地方并禁用此输出。

What makes sense, however, is redirect its error output into the file in case there are errors. You might want to disable standard output if you want.

然而,有意义的是将其错误输出重定向到文件中,以防出现错误。如果需要,您可能希望禁用标准输出。

There are two possible ways of doing this. First is to invoke your script with redirection of both stderr and stdout into the log file:

有两种可能的方法来做到这一点。首先是通过将 stderr 和 stdout 重定向到日志文件来调用您的脚本:

./myscript.sh >log 2>&1

Second, is to tell bash to do this right in your script:

其次,是告诉 bash 在您的脚本中正确执行此操作:

#!/bin/sh

exec 2>&1

if $C_SFTP; then
   scp -r $C_SFTP_USER@$C_SFTP_HOST:$C_SOURCE "$C_TMPDIR"
fi

...

If you need to check for errors, just verify that $?is 0after scp command is executed:

如果您需要检查错误,只是确认$?0执行scp命令后:

if $C_SFTP; then
   scp -r $C_SFTP_USER@$C_SFTP_HOST:$C_SOURCE "$C_TMPDIR"
   RET=$?
   if [ $RET -ne 0 ]; then
      echo SOS 2>&1
      exit $RET
   fi
fi

Another option is to do set -ein your script which tells bash script to report failure as soon as one of commands in scripts fails:

另一种选择是set -e在您的脚本中执行,它告诉 bash 脚本在脚本中的一个命令失败时立即报告失败:

#!/bin/bash

set -e

...

Hope it helps. Good luck!

希望能帮助到你。祝你好运!

回答by Netzschrauber

You cat simply test your tty with:

您只需使用以下命令测试您的 tty:

[ ~]#echo "hello" >/dev/tty
hello

If that works, try:

如果可行,请尝试:

[ ~]# scp <user>@<host>:<source> /dev/tty 2>/dev/null

This has worked for me...

这对我有用...

回答by Pada

Unfortunately SCP's output can't simply be redirected to stdout it seems.

不幸的是,SCP 的输出似乎不能简单地重定向到标准输出。

I wanted to get the average transfer speed of my SCP transfer, and the only way that I could manage to do that was to send stderr and stdout to a file, and then to echo the file to stdout again.

我想获得 SCP 传输的平均传输速度,而我能做到的唯一方法是将 stderr 和 stdout 发送到一个文件,然后再次将文件回显到 stdout。

For example:

例如:

#!/bin/sh
echo "Starting with upload test at `date`:"

scp -v -i /root/.ssh/upload_test_rsa /root/uploadtest.tar.gz speedtest@myhost:/home/speedtest/uploadtest.tar.gz > /tmp/scp.log 2>&1
grep -i bytes /tmp/scp.log
rm -f /tmp/scp.log

echo "Done with upload test at `date`."

Which would result in the following output:

这将导致以下输出:

Starting with upload test at Thu Sep 20 13:04:44 SAST 2012:
Transferred: sent 10191920, received 5016 bytes, in 15.7 seconds
Bytes per second: sent 650371.2, received 320.1
Done with upload test at Thu Sep 20 13:05:04 SAST 2012.

回答by MatthewD

I found a rough solution for scp:

我为 scp 找到了一个粗略的解决方案:

$ scp -qv $USER@$HOST:$SRC $DEST

According to the scp man page, -q(quiet) disables the progress meter, as well as disabling all other output. Add -v(verbose) as well, you get heaps of output... and the progress meter is still disabled! Disabling the progress meter allows you to redirect the output to a file.

根据 scp 手册页,-q(quiet) 禁用进度表,以及禁用所有其他输出。添加-v(详细),你会得到大量的输出......并且进度表仍然被禁用!禁用进度表允许您将输出重定向到文件。

If you don't need all the authentication debug output, redirect the output to stdout and grep out the bits you don't want:

如果您不需要所有身份验证调试输出,请将输出重定向到 stdout 并 grep 出您不想要的位:

$ scp -qv $USER@$HOST:$SRC $DEST 2>&1 | grep -v debug

Final output is something like this:

最终输出是这样的:

Executing: program /usr/bin/ssh host myhost, user (unspecified), command scp -v -f ~/file.txt
OpenSSH_6.0p1 Debian-4, OpenSSL 1.0.1e 11 Feb 2013
Warning: Permanently added 'myhost,10.0.0.1' (ECDSA) to the list of known hosts.
Authenticated to myhost ([10.0.0.1]:22).
Sending file modes: C0644 426 file.txt
Sink: C0644 426 file.txt
Transferred: sent 2744, received 2464 bytes, in 0.0 seconds
Bytes per second: sent 108772.7, received 97673.4

Plus, this can be redirected to a file:

另外,这可以重定向到一个文件:

$ scp -qv $USER@$HOST:$SRC $DEST 2>&1 | grep -v debug > scplog.txt