我正在编写一个脚本来帮助我在家用电脑和服务器之间同步文件。我遇到的问题之一是,当我运行 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
}