Srik
Srik

Reputation: 2381

unexpected EOF while looking for matching `"'

The command works fine when I execute from command line. However It throws an error when I execute it from shell script

rsync -avz -e ssh --exclude-from=rsync.file --rsync-path="sudo rsync" ostnfe/ [email protected]:/var/www/ostnfe

Code from shell script:

CMD='rsync -avz -e ssh --exclude-from=rsync.file --rsync-path="sudo rsync"  '$1'/ ubuntu@'$AMZ':/var/www/'$2
$CMD

error:

bash: -c: line 0: unexpected EOF while looking for matching `"'
bash: -c: line 1: syntax error: unexpected end of file

Upvotes: 1

Views: 13001

Answers (3)

Andreas Bombe
Andreas Bombe

Reputation: 2470

You could just use a shell function instead:

cmd () {
    rsync -avz -e ssh --exclude-from=rsync.file --rsync-path="sudo rsync"  $1/ ubuntu@$AMZ:/var/www/$2
}

# calling with args
cmd "$1" "$2"

# alternatively, calling through variable without args
VAR='eval cmd "$1" "$2"'
$VAR

Less hassle with escaping this way.

Update: Edited cmd() to represent the working solution.

Upvotes: 2

PradyJord
PradyJord

Reputation: 2160

Please try following:

CMD=$(echo "rsync -avz -e ssh --exclude-from=rsync.file --rsync-path=\"sudo rsync\"  $1/ubuntu@${AMZ}:/var/www/$2")

$CMD

check for any missing or extra space.

Upvotes: 0

anubhava
anubhava

Reputation: 784918

Bad idea to store full command line in a string. Use BASH arrays instead:

CMD=(rsync -avz -e ssh --exclude-from=rsync.file "--rsync-path='sudo rsync'" "$1/ ubuntu@$AMZ:/var/www/$2")
"${CMD[@]}"

Upvotes: 0

Related Questions