2

我正在编写一个脚本来帮助我在家用电脑和服务器之间同步文件。我遇到的问题之一是,当我运行 for 循环时,它会输出空行。

其他一切似乎都很好,echo $REMOTE按预期列出了所有文件及其完整路径。我尝试过取消引用、引用、将 EOF 从“EOF”更改为 EOF 等,但到目前为止没有任何效果。

这是下面的脚本:

function uploadDownload()
{
    if [ "$1" == "d" ]; then
        ssh -i ~/Dropbox/Business/aws/first.pem ubuntu@XXXX.194.202 << EOF
        echo $REMOTE

        for file in $REMOTE; do
            echo $file
        done
EOF
        #scp -r -i ~/Dropbox/Business/aws/first.pem ubuntu@XXXX.194.202:$REMOTE $LOCAL
    elif [ "$1" == "u" ]; then
        scp -r -i ~/Dropbox/Business/aws/first.pem $LOCAL ubuntu@XXXX.194.202:$REMOTE
    fi
}


if [ "$2" == "m" ]; then

    if [ -z "$3" ]; then
        echo "Please enter the local location of the files and provide an absolute path..."
        read LOCAL
        echo "Please enter the remote location of the files..."
        read REMOTE
        uploadDownload
    else
        LOCAL=$2
        if [ -z "$4" ]; then
            REMOTE=$3
        else
            REMOTE='ubuntu@XXXX.194.202:~/test/'
        fi  
        uploadDownload
    fi
else
    LOCAL='/home/will/Dropbox/Business/aws/files/binaryhustle/'
    REMOTE='/home/ubuntu/dev/binaryhustle/*'
    uploadDownload $1
fi

编辑:根据建议,我编辑了 uploadDownload 函数。我还应该注意,我echo "$REMOTE";在循环之外进行了测试,它什么都不返回(不像没有引号,它会返回一些东西)。

function uploadDownload()
    {
        if [ "$1" == "d" ]; then
            ssh -i ~/Dropbox/Business/aws/first.pem ubuntu@54.252.194.202 << EOF
    for file in "/home/ubuntu/dev/binaryhustle"; do
    echo $file
    done
    EOF
            #scp -r -i ~/Dropbox/Business/aws/first.pem ubuntu@54.252.194.202:$REMOTE $LOCAL
        elif [ "$1" == "u" ]; then
            scp -r -i ~/Dropbox/Business/aws/first.pem $LOCAL ubuntu@54.252.194.202:$REMOTE
        fi
    }
4

3 回答 3

2

尝试引用您的变量$file,因为它在被发送到远程服务器之前被扩展:

            echo $file

            echo \$file

或者更好

            echo \"\$file\"
于 2013-09-07T09:38:22.330 回答
1

你可能想看看unison。其强大的文件同步工具能够通过 ssh 工作。它虽然没有任何内容差异。

于 2013-09-07T10:12:26.167 回答
0

感谢@konsolebox让我走到了一半,我通过在文件中添加斜杠来让它工作,但是这不适用于 << EOF ,只有 <

function uploadDownload()
{
    if [ "$1" == "d" ]; then
            ssh -i ~/Dropbox/Business/aws/first.pem ubuntu@54.252.194.202 <<EOF
for file in "$REMOTE"; do
echo \$file
done
EOF
            #scp -r -i ~/Dropbox/Business/aws/first.pem ubuntu@54.252.194.202:$REMOTE $LOCAL
        elif [ "$1" == "u" ]; then
            scp -r -i ~/Dropbox/Business/aws/first.pem $LOCAL ubuntu@54.252.194.202:$REMOTE
        fi
    }
于 2013-09-07T10:02:23.993 回答