在bash变量中使用--filter的rsync命令

时间:2014-01-15 21:04:05

标签: bash variables rsync quoting

这类似于我在stackoverflow上看到的一些其他rsync bash变量问题,但这些答案都没有解决我的具体问题。我想在bash脚本中运行以下命令,但是rsync选项包含在变量中:

rsync -ahz --delete --filter=': .rsync-filter' source/ destination/

我注意到bash实际上解释如下:

+ rsync -ahz --delete '--filter=: .rsync-filter' source/ destination/

在rsync中也可以正常工作。

当我尝试将这些值放入变量时,我看到正在运行以下命令:

RSYNCOPTS="-ahz --delete --filter=: .rsync-filter"
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete --filter=: .rsync-filter'
+ rsync -ahz --delete --filter=: .rsync-filter source/ destination/
unexpected end of filter rule: :

RSYNCOPTS="-ahz --delete --filter=': .rsync-filter'"
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete --filter='\'': .rsync-filter'\'''
+ rsync -ahz --delete '--filter='\'':' '.rsync-filter'\''' source/ destination/
Unknown filter rule: `':'

RSYNCOPTS="-ahz --delete '--filter=: .rsync-filter'"
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete '\''--filter=: .rsync-filter'\'''
+ rsync -ahz --delete ''\''--filter=:' '.rsync-filter'\''' source/ destination/
Unexpected local arg: .rsync-filter'

RSYNCOPTS='-ahz --delete --filter=: .rsync-filter'
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete --filter=: .rsync-filter'
+ rsync -ahz --delete --filter=: .rsync-filter source/ destination/
unexpected end of filter rule: :

RSYNCOPTS='-ahz --delete --filter=": .rsync-filter"'
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete --filter=": .rsync-filter"'
+ rsync -ahz --delete '--filter=":' '.rsync-filter"' source/ destination/
Unknown filter rule: `":'

RSYNCOPTS='-ahz --delete "--filter=: .rsync-filter"'
rsync  ${RSYNCOPTS} source/ destination/
+ RSYNCOPTS='-ahz --delete "--filter=: .rsync-filter"'
+ rsync -ahz --delete '"--filter=:' '.rsync-filter"' source/ destination/
Unexpected local arg: .rsync-filter"

我甚至尝试了一些数组,看看是否有帮助:

RSYNCOPTS=(-ahz --delete '--filter=: .rsync-filter')
rsync  ${RSYNCOPTS[@]} source/ destination/
+ RSYNCOPTS=(-ahz --delete '--filter=: .rsync-filter')
+ rsync -ahz --delete --filter=: .rsync-filter source/ destination/
unexpected end of filter rule: :

RSYNCOPTS=(-ahz --delete '--filter=": .rsync-filter"')
rsync  ${RSYNCOPTS[@]} source/ destination/
+ RSYNCOPTS=(-ahz --delete '--filter=": .rsync-filter"')
+ rsync -ahz --delete '--filter=":' '.rsync-filter"' source/ destination/
Unknown filter rule: `":'

RSYNCOPTS=(-ahz --delete "--filter=': .rsync-filter'")
rsync  ${RSYNCOPTS[@]} source/ destination/
+ RSYNCOPTS=(-ahz --delete "--filter=': .rsync-filter'")
+ rsync -ahz --delete '--filter='\'':' '.rsync-filter'\''' source/ destination/
Unknown filter rule: `':'

那我错过了什么?

1 个答案:

答案 0 :(得分:7)

使用数组方法和双引号数组引用:

RSYNCOPTS=(-ahz --delete '--filter=: .rsync-filter')
rsync  "${RSYNCOPTS[@]}" source/ destination/