当在.bashrc中使用echo时,SCP无法工作?

87

我在Fedora有两个用户:

  1. Wani
  2. root(显而易见!)

用户Wani的.bashrc文件内容如下:

# .bashrc
echo "Hello"
# Source global definitions
if [ -f /etc/bashrc ]; then
    . /etc/bashrc
fi

# User specific aliases and functions

现在在以root身份登录后,我输入以下命令:

[root@Dell Wani]# touch try.txt
[root@Dell Wani]# service sshd start
[root@Dell Wani]# scp try.txt Wani@localhost:~/
Wani@localhost's password: 
Hello
[root@Dell Wani]# 

现在我登录到Wani,并键入:

[Wani@Dell ~]$ cat try.txt
cat: try.txt: No such file or directory
[Wani@Dell ~]$ 

现在我再次以root身份登录,并输入同样的命令,加上-v参数:

[root@Dell Wani]# scp -v morph.log Wani@localhost:
Executing: program /usr/bin/ssh host localhost, user Wani, command scp -v -t -- .
OpenSSH_5.6p1, OpenSSL 1.0.0j-fips 10 May 2012
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: permanently_set_uid: 0/0
debug1: identity file /root/.ssh/id_rsa type -1
debug1: identity file /root/.ssh/id_rsa-cert type -1
debug1: identity file /root/.ssh/id_dsa type -1
debug1: identity file /root/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.6
debug1: match: OpenSSH_5.6 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'localhost' is known and matches the RSA host key.
debug1: Found key in /root/.ssh/known_hosts:2
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-     with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
debug1: Next authentication method: gssapi-with-mic
debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found

debug1: Unspecified GSS failure.  Minor code may provide more information
Credentials cache file '/tmp/krb5cc_0' not found

debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Unspecified GSS failure.  Minor code may provide more information


debug1: Next authentication method: publickey
debug1: Trying private key: /root/.ssh/id_rsa
debug1: Trying private key: /root/.ssh/id_dsa
debug1: Next authentication method: password
Wani@localhost's password: 
debug1: Authentication succeeded (password).
Authenticated to localhost ([127.0.0.1]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env XMODIFIERS = @im=none
debug1: Sending env LANG = en_US.UTF-8
debug1: Sending command: scp -v -t -- .
Hello
[root@Dell Wani]# debug1: client_input_channel_req: channel 0 rtype exit-status      reply      0
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 1664, received 1976 bytes, in 0.1 seconds
Bytes per second: sent 22961.5, received 27266.8
debug1: Exit status 0

(当我按下回车键后)

[root@Dell Wani]# 

有谁能够解释一下这里到底发生了什么?为什么文件没有从根目录复制到Wani?


cp怎么样?cp try.txt ~Wani/可以吗? - nneonneo
1
确实有可能,交互式命令或来自您的.bashrc的意外输出可能会破坏某些非交互式场景。尝试将代码条件化为case $- in *i*或类似内容。 - tripleee
@nneonneo:可以的。实际上,即使我有两个用户在不同的计算机上,文件也不会被复制! - Nehal J Wani
3
我看到的所有帖子都描述了为什么在各种bashrc文件中存在这个问题。然而,当你使用SCP / STFP连接到一台你不拥有且无法更改启动脚本的机器时,那么解决方案是什么? - ChuckB
1
我最终将所有的回显重定向到了stderr。虽然能用,但是... - Andreas
10个回答

84
.bashrc 中使用 echo 将会破坏 scp,因为 scp 期望在 stdin/stdout 通道上看到其协议数据。有关此问题的更多讨论,请参见https://bugzilla.redhat.com/show_bug.cgi?id=20527
有几个解决方法可用:
  • 根据“交互”标志进行条件判断(例如,像 tripleee 建议的那样,case $- in *i*
  • 使用 tty 实用程序检测交互式 shell(例如,if tty > /dev/nullif [ -t 0 ]
  • 检查 $SSH_TTY 的值
我想你应该使用适合你的任何一个解决方法。不幸的是,我不知道最好的(最便携/最可靠)选项是什么。

6
为了澄清,“case”命令应该是:case $- in *i*) echo 此处安全,因为shell是交互式的;esac - mlathe
shopt 也是一种替代方法。 - vesperto
1
由于我的主机始终通过SSH访问,因此SSH_TTY始终设置。对我来说,使用“-t 0”条件效果很好。现在我在我的bashrc中到处使用以下函数。 if [[ -t 0 ]];then echo -e "$*" fi } - Sandip Bhattacharya
当您首次通过ssh连接到某个主机,然后执行scp操作时,例如:$ ssh myhost $ scp myfile anotherhost:~由于$SSH_TTY在第一次ssh时被设置,并且在随后的命令中从未被重置,因此$SSH_TTY检查将无法正常工作。 - Robin Hsu
你刚刚帮我省去了很多的挫败感,谢谢你。 - O.rka
显示剩余2条评论

20

除了nneonneo提供的选项,你还可以使用交互标志进行条件操作:

if [[ $- =~ "i" ]]

我认为这可能是Bash中最清晰的方法。


[[ 不是可移植的,我也不认为它是 POSIX 的。在 dash shell 中尝试这个命令:if [[ 0 = 0 ]]; then echo x; else echo y; fi。你会得到 dash: 3: [[: not found 的错误提示。 - Elliptical view

18

13

Ubuntu的默认.bashrc包含以下片段,已经解决了这个问题:

# If not running interactively, don't do anything
case $- in
    *i*) ;;
    *) return;;
esac

4
[[ $- != *i* ]] && return 的意思是:如果当前shell不是交互式的,则退出。 - phil294
3
您答案中提供的表单是符合POSIX标准的。而您在评论中所提供的内容由于使用了[[特性,因此不符合POSIX标准。对于bashrc文件来说并不成问题;但如果您想在其他上下文/Shell中使用它,可能会存在问题。 - Totor

4
.bashrc中,使用 STDERR 作为输出:
echo "# Important Notice" >&2

更新:不要使用它! 最近我们遇到了一个问题,一个(闭源)工具因为.bashrc中的echo输出到了 STDERR 而失败了。这个工具(使用rcp)期望没有任何输出,无论是在 STDOUT 还是 STDERR 上。当它遇到 echo 时就卡住了。教训是:为人类和机器(脚本)分别创建独立的账户,或者停止通过.bashrc说闲话。


为人类和机器分别创建独立的账户是被低估的。 - Andreas

2
似乎最便携的测试交互式shell的方法是:
test -t 0
if [ $? -eq 0 ]
then
    # interactive
    ;
else
    # non-interactive
    ;
fi

1

nneonneo的解决方案对我也起作用了。但由于我的默认shell是TCSH,所以我必须稍微编辑一下修复方法(在.tcshrc中):

if ( $?SSH_TTY ) then
    exec /bin/bash
endif

我想分享给大家,以造福大家。


我有cshrc文件,你的解决方案对我有效。我已经点赞了。 - King

0
如果您使用的是Red Hat Enterprise Linux(RHEL)或其变体,请将一个脚本放入/etc/profile.d/中,该脚本可以执行echo或其他您想要的操作。

0
如果你想要输出语句,可以在 @Blauhirn 的回答中的 case 条件之后添加你的 echo 语句。
case $- in
    *i*) ;;
    *) return;;
esac

echo "Your Greeting/Warning Message/s here!"

-1
if [ 0 -eq $(shopt -q login_shell; echo $?) ]; then
  echo "do something?"
fi

源代码


为 OP 提供一些背景信息会很好。 - allanberry
上下文和相关信息已经可以从其他答案和问题本身中提取出来。这只是另一种已经理解的方式,信噪比低。 - vesperto

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接