Finally got it working. Now it can handle strings like test"!'試みる.ext
. I'm using ssh and tar now, but should also work with rsync.
sync {
item=${1//\"/\\\"}
ssh -n -c arcfour $server tar -C \"$remotedir\" -c -- \"$item\" | tar x 2>/dev/null
}
Question
I'm writing a script for syncing completed torrents from a remote server and remembering already synced files/folders. But filenames with quotes or japanese letters are causing some trouble, so rsync have to run two times at the moment.
rsync -aP ovh:"$src/$(printf "%q" "$1")" "$dst/"
working:
not working:
Every single japanese letter will be converted to something like ?\#211
, \#215島
or \#227?\
. Since I'm using OS X, I also tried --iconv=utf-8-mac,utf-8
- without success.
rsync -aP ovh:"'$src/${1/\'/\\'}'" "$dst/"
working:
not working:
For example $1
is test's file
, the string send to the server becomes "'/data/rtorrent/complete/test\'s file'"
Error message: zsh:1: unmatched '
Seems like escaping within a single quoted text doesn't work. But if the outer single quotes are removed, rsync interprets a whitespace as separator for another file.
I thought maybe it would help to convert every single character to unicode (like \u1337
) and send this string to the server, but didn't find a way to do that. Just endless scripts for the other way around.
Also sed wasn't helpful either - way too much work to escape everything manually. This script should work reliable and I don't want to check every now and then if I covered every possibility which maybe needs escaping.
Any idea how to merge the two commands into one?
Edit: my temporary solution was this:
sync() {
# 1. escape quotes / 2. escape kana
rsync -aP ovh:"$src/$(printf "%q" "$1")" "$dst/" >& /dev/null && success "$1" || \
(rsync -aP ovh:"'$src/${1/\'/\\'}'" "$dst/" >& /dev/null && success "$1" || unlock "$1")
}
Solution
Finally got it working. Now it can handle strings like test"!'試みる.ext
. I'm using ssh and tar now, but should also work with rsync.
sync {
item=${1//\"/\\\"}
ssh -n -c arcfour $server tar -C \"$remotedir\" -c -- \"$item\" | tar x 2>/dev/null
}