Ssh and sftp via here document

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I am trying to automate a file transfer from a remote (vendor's)
machine.  The file transfer is performed by sftp during an ssh
session in which the user running the script temporarily becomes
the same user as on the remote machine, so that authentication
can be done without storing cleartext passwords in the script.

The heart of the script looks like this (some names have been

echo "1 $(whoami)" >>$LOGDIR/$LOGFILE

echo "2 $(whoami)" >>$LOGDIR/$LOGFILE
lcd  $EDIR
get  *.DSB
echo "3 $(whoami)" >>$LOGDIR/$LOGFILE
if [ -r *.DSB ]
then /sbin/chown xxxxxx:yyyyyy $EDIR/*.DSB
echo "4 $(whoami)" >>$LOGDIR/$LOGFILE
echo "5 $(whoami)" >>$LOGDIR/$LOGFILE

cd $EDIR
if [ -r *.DSB ]
then for i in *.DSB
     do  dsbcount=$(expr $dsbcount + 1)
         echo "Downloaded $i to $FDIR" >>$LOGDIR/$LOGFILE
         mv $i $FDIR/$i
echo "6 $(whoami)" >>$LOGDIR/$LOGFILE

I have verified that all the shell variables get set to the proper
values; they are all echoed to $LOGDIR/$LOGFILE in a part of the
script I didn't show.  A relevant part of the $LOGFILE is shown

1 xxxxxx
2 xxxxxx
5 xxxxxx
Downloaded disbursement roster FV.DSB to /xxx/yyy/zzz
6 xxxxxx
-rw-r--r--   1 $USERID   $ELMGROUP         6 Sep  2 09:21 $FDIR/FV.DSB

At 1, the user is xxxxxx, as expected.  At 2, the user should
be $USERID, but is not.  Note that echo 3 and echo 4 do not
appear; also, the requested directory listing is not printed.  I
expected xxxxxx to again be the user at echo 5, and that is the
case.  Echo 6 again shows the user is xxxxxx, as expected, and the
ll command shows that the chown command has not succeeded.

Everything works as I expect when I issue the ssh and sftp commands
manually from a shell prompt, logged in as xxxxxx.  That is, whoami
issued at the echo 2 point is $USERID, and the chown command

Can someone explain what is happening?  And tell me how to get the
expected output?

Background information: The operating system is HPUX 11i Version 1.
Ssh is the OpenSsh 4.2 rebranded as an HPUX product.  The default
shell is the posix shell.

Many thanks,


Re: Ssh and sftp via here document

In article
Quoted text here. Click to load it

You'd probably be better off asking these questions in

Quoted text here. Click to load it

From a random sh(1) man page:

     The following redirection is often called a ``here-document''.

           [n]<< delimiter

     All the text on successive lines up to the delimiter is saved away and
     made available to the command on standard input, or file descriptor n if
     it is specified.  If the delimiter as specified on the initial line is
     quoted, then the here-doc-text is treated literally, otherwise the text
     is subjected to parameter expansion, command substitution, and arithmetic
     expansion [...]

I.e. all the stuff in your here document is expandend by the shell
running the script, before being passed to ssh.

Quoted text here. Click to load it

As expected per above.

Quoted text here. Click to load it

Probably sftp ate everything up to EOF, and threw the stuff after "bye"
away. You can trust /bin/sh to do "line-buffered reads", but not most
other programs.

Shell scripting with nested programs reading from the same here
document, if at all possible, is not for the faint of heart. I suggest
that you make use of files instead as far as possible (including via the
-b option to sftp).

--Per Hedeland

Site Timeline