I use this bash code to upload files to a remote server, for normal files this works great:
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, for-loop fails, so I replaced the first line as follows:
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, so the first missing directory is created perfectly, but all subsequent missing files / directories are ignored.
I assume this is due to the fact that ssh writes something to stdout, which confuses the read command. Commenting on the ssh command, the loop works as it should.
Does anyone know why this is happening, and how can ssh be prevented from breaking a while loop?
bash ssh
Robby75 Feb 22 '12 at 10:28 2012-02-22 10:28
source share