Reputation: 2381
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
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
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
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