2

I am writing a script to help me sync files between my home pc and my server. One of the problems ive run into is that, when I run the for loop, it outputs blank lines.

Everything else seems to be fine, echo $REMOTE lists all files and their full paths as expected. I have tried unquoting, quoting, Changing EOF from "EOF" to EOF etc, but so far nothing has worked.

Here is the script below:

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

        for file in $REMOTE; do
            echo $file
        done
EOF
        #scp -r -i ~/Dropbox/Business/aws/first.pem [email protected]:$REMOTE $LOCAL
    elif [ "$1" == "u" ]; then
        scp -r -i ~/Dropbox/Business/aws/first.pem $LOCAL [email protected]:$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='[email protected]:~/test/'
        fi  
        uploadDownload
    fi
else
    LOCAL='/home/will/Dropbox/Business/aws/files/binaryhustle/'
    REMOTE='/home/ubuntu/dev/binaryhustle/*'
    uploadDownload $1
fi

EDIT: based on advice I have edited the uploadDownload function. I should also note than I tested echo "$REMOTE"; outside of the loop and it returns nothing (unlike without quotes, where it returns something).

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

3 Answers 3

2

Try to quote your variable $file as it is expanded before being sent to the remote server:

            echo $file

To

            echo \$file

Or better yet

            echo \"\$file\"
Sign up to request clarification or add additional context in comments.

Comments

1

You may want to take a look at unison. Its powerful file syncing tool able to work over ssh. It doesn't do any content diff though.

1 Comment

Thanks I actually ended up using rsync. I will post the final code for others.
0

Credit to @konsolebox for getting me half way there, I got it working by adding the slash to file, however this did not work with << EOF , only <

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

3 Comments

Did you try not to indent your delimiter EOF?. Also since you quoted $REMOTE this time ("$REMOTE"), it would just expand to one word. I may have suggested quoting your variables before but not for one you really intend to be processed with word splitting. (Edit: Arrays won't be applicable to that. One added note deleted.)
And please tell us why it still doesn't work, and what mod actually worked. I read it as you used <EOF instead?
Yes the delimiter is not indented, removing the space between << and EOF is what fixed it (in addition to adding the slash) <<EOF instead of << EOF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.