I use this bash-code to upload files to a remote server, for normal files this works fine:
for i in `find devel/ -newer $UPLOAD_FILE`
do
echo "Upload:" $i
if [ -d $i ]
then
echo "Creating directory" $i
ssh $USER@$SERVER "cd ${REMOTE_PATH}; mkdir -p $i"
continue
fi
if scp -Cp $i $USER@$SERVER:$REMOTE_PATH/$i
then
echo "$i OK"
else
echo "$i NOK"
rm ${UPLOAD_FILE}_tmp
fi
done
The only problem is that for files with a space in the name, the for-loop fails, so I replaced the first line like this:
find devel/ -newer $UPLOAD_FILE | while read i
do
echo "Upload:" $i
if [ -d $i ]
then
echo "Creating directory" $i
ssh $USER@$SERVER "cd ${REMOTE_PATH}; mkdir -p $i"
continue
fi
if scp -Cp $i $USER@$SERVER:$REMOTE_PATH/$i
then
echo "$i OK"
else
echo "$i NOK"
rm ${UPLOAD_FILE}_tmp
fi
done
For some strange reason, the ssh-command breaks out of the while-loop, therefore the first missing directory is created fine, but all subsequent missing files/directories are ignored.
I guess this has something to do with ssh writing something to stdout which confuses the "read" command. Commenting out the ssh-command makes the loop work as it should.
Does anybody know why this happens and how one can prevent ssh from breaking the while-loop?
while readwill break badly when your filenames contain literal backslashes. Safer to useread -r. Similarly,readwill strip trailing whitespace from names; to avoid that, you need to clearIFS.find | while reademits a newline-delimited stream, but legitimate filenames on UNIX are allowed to contain newlines. Think about what happens if someone does amkdir -p devel/$'\n'/etcand then writes todevel/$'\n'/etc/passwd; you'd better hope at this point that your script doesn't have permission to write to/etc/passwdon the remote machine.[ -d $i ]will misbehave if$icontains spaces or a wildcard expression, or is empty. Either use[[ -d $i ]](if your shell is bash or another ksh derivative) or[ -d "$i" ], with the quotes (for POSIX compatibility).sshsuccessfully makes a connection with the remote machine. It does not happen when the connection is refused.