Frage

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/ ubuntu@mask.compute-1.amazonaws.com:/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
War es hilfreich?

Lösung

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.

Andere Tipps

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[@]}"

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top