开发者

rsync option in a variable

开发者 https://www.devze.com 2023-01-16 08:57 出处:网络
I want to put command option of rsync into a variable so I can reuse it for other rsync commands. Here is what I tried but it didn\'t work.

I want to put command option of rsync into a variable so I can reuse it for other rsync commands. Here is what I tried but it didn't work.

roption="-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class'"
rsync "$roption" /media/CORSAIR/workspace ~/

Can any body help me figure开发者_StackOverflow中文版 out the problem?

Thanks,


Use shell arrays. They're extremely useful if you want to form strings using escapes and have them be literally what is typed. Plus, security.

roption=(
    -a
    --recursive
    --progress
    --exclude='class'
    --delete
    --exclude='exclude'
    --exclude='.svn'
    --exclude='.metadata'
    --exclude='*.class'
)

rsync "${roption[@]}" /media/CORSAIR/workspace ~/

You can even add to them:

if [ "$VERBOSE" -ne 0 ]; then
    roption+=(--verbose)
fi


Since your $roption represents more than one argument, you should use $roption, not "$roption".

Of course, using a scalar to hold multiple values is just wrong. If you are using bash, consider using an array instead:

roptions=(-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class')
rsync "${roptions[@]}" /media/CORSAIR/workspace ~


You can try the 'eval' command, which will ask the shell to parse the command line once before eval gets to interpret it:

roption="-a --recursive --progress --exclude='class' --delete --exclude='exclude' --exclude='.svn' --exclude='.metadata' --exclude='*.class'" 

eval "rsync $roption /media/CORSAIR/workspace ~"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号