4

Ok, so I have used linux for a long time but this is the weirdest thing I have encountered in a long time. Maybe I just don't do a lot of scp so probably missing something simple here. So in my .bashrc file I have the following.

export KERNEL=$(uname -r)
#echo "HOST: $HOSTNAME: KERNEL VERSION: $KERNEL"
if [ $KERNEL == "2.6.32-431.3.1.el6.x86_64" ];then
   ... logic here
else
    .. more logic
fi

Now if I uncomment that echo line then the following command doesn't work! It just shows the echo but doesn't do any copying. If I remove that line it does the copy

$ scp -r host1.net:/prod/path1/path2/dir1/etc /tmp/user/sim/dir1
HOST: host1.net: KERNEL VERSION: 3.4.70-1.el6.companyX


$ scp -r host1.net:/prod/path1/path2/dir1/etc /tmp/user/sim/dir1

pybackup 100% 174 0.2KB/s 00:00
Proxy.ini 100% 623 0.6KB/s 00:00

This is strange...

bjackfly
  • 203

2 Answers2

2

Using echo in a .bashrc will break scp, as scp expects to see its protocol data over the stdin/stdout channels. See https://bugzilla.redhat.com/show_bug.cgi?id=20527 and this post for more discussion on this issue.

There's a few workarounds available, how to use echo in a scp-safe manner in .bashrc:

case $- in *i*) echo Hello World ; esac
tty >/dev/null && echo Hello World
if [ -t 1 ]; then echo Hello World ; fi
1

Here are the first lines of my (default) .bashrc file:

# ~/.bashrc: executed by bash(1) for non-login shells.
# see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
# for examples

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

The check for an interactive session avoids messing up with SCP, SFTP or ssh remote-host command mode.

Without this, if your file .bashrc uses echo or some other stuff printing on stdout, you may get this kind of errors:

  • SFTP : Received message too long 168435779
  • SCP : protocol error: unexpected <newline>
Totor
  • 1,581