SSH会中断在bash while循环的 [英] ssh breaks out of while-loop in bash

查看:201
本文介绍了SSH会中断在bash while循环的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个bash- code将文件上传到远程服务器,对于正常的文件,这工作得很好:

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

唯一的问题是,在名称中有空格的文件,for循环失败,所以我取代了第一行是这样的:

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

有关一些奇怪的原因,SSH命令就会中断while循环,因此第一个丢失目录中创建罚款,但随后的所有丢失的文件/目录将被忽略。的

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.

我想这牵扯到使用ssh写的东西到stdout其迷惑读命令。注释掉SSH命令,使循环的工作,因为它应该。

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.

是否有人知道为什么会这样,一个如何从打破while循环prevent SSH?

Does anybody know why this happens and how one can prevent ssh from breaking the while-loop?

推荐答案

的问题是, SSH 从标准输入读取,因此,吃你的全部剩余线路。您可以从标准输入设备只要连接到无处:

The problem is that ssh reads from standard input, therefore it eats all your remaining lines. You can just connect its standard input to nowhere:

ssh $USER@$SERVER "cd ${REMOTE_PATH}; mkdir -p $i" < /dev/null

您也可以使用 SSH -n 而不是重定向。

You can also use ssh -n instead of the redirection.

这篇关于SSH会中断在bash while循环的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆