Bash:需要帮助,将变量传递给rsync [英] Bash: need help passing a a variable to rsync

查看:88
本文介绍了Bash:需要帮助,将变量传递给rsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 $ home 作为运行 rsync 时要排除的目录列表.文件:

I want to pass $home as a list of directories to be excluded when running rsync. The file:

cat backup_system.sh
#!/bin/bash
set -x
home=''
home=$home'"/home/ravas/Documents/*"',
home=$home'"/home/ravas/Downloads/*"',
home=$home'"/home/ravas/pCloudDrive/*"',
home=$home'"/home/ravas/pCloudLocal/*"',
home=$home'"/home/ravas.old/*"'
echo $home
sudo rsync -aAXv / --exclude={$home,"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /run/media/ravas/system_backup

在没有 $ home 变量的情况下,备份运行良好,但是如果添加它,则没有任何效果.

The backup runs fine without the $home variable, but if I add it, it doesn't have any effect.

输出的最后两行显示 $ home 中列出的目录未使用-exclude 进行扩展,而其他目录则进行了扩展.

The last two lines of the output show that the directories listed in $home are not expanded with --exclude, whereas the others are.

不用说,我不太擅长bash,任何帮助将不胜感激!

Needless to say, I'm not very good at bash, and any help would be much appreciated!

./backup_system.sh 
+ home=
+ home='"/home/ravas/Documents/*",'
+ home='"/home/ravas/Documents/*","/home/ravas/Downloads/*",'
+ home='"/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*",'
+ home='"/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*","/home/ravas/pCloudLocal/*",'
+ home='"/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*","/home/ravas/pCloudLocal/*","/home/ravas.old/*"'
+ echo '"/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*","/home/ravas/pCloudLocal/*","/home/ravas.old/*"'
"/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*","/home/ravas/pCloudLocal/*","/home/ravas.old/*"
+ sudo rsync -aAXv / 
'--exclude="/home/ravas/Documents/*","/home/ravas/Downloads/*","/home/ravas/pCloudDrive/*","/home/ravas/pCloudLocal/*","/home/ravas.old/*"' 
'--exclude=/dev/*' '--exclude=/proc/*' '--exclude=/sys/*' '--exclude=/tmp/*' '--exclude=/run/*' '--exclude=/mnt/*' '--exclude=/media/*' --exclude=/lost+found** /run/media/ravas/system_backup

推荐答案

基本问题是bash解析命令中各种内容的顺序对于您尝试执行的操作都是错误的.具体来说,它解析(并应用和删除)引号&转义,然后扩展{this,that,theother}列表,然后扩展变量引用(如$home).最终结果是,当$home扩展为逗号分隔的引用元素列表时,对于引号或逗号而言,达到预期的效果为时已晚.

The basic problem is the order that bash parses various things in your command is all wrong for what you're trying to do. Specifically, it parses (and applies and removes) quotes & escapes, then expands {this,that,theother} lists, then expands variable references (like $home). Net result is that by the time $home expands to a comma-separated list of quoted elements, it's too late for either the quotes or commas to have their intended effect.

通常在这种情况下,数组是处理此问题的最佳方法:

As usual in cases like this, arrays are the best way to handle this:

homearray=()
homearray+=("/home/ravas/Documents/*")
homearray+=("/home/ravas/Downloads/*")
homearray+=("/home/ravas/pCloudDrive/*")
homearray+=("/home/ravas/pCloudLocal/*")
homearray+=("/home/ravas.old/*")
# Alternate short form:
# homearray=(/home/ravas/{Documents,Downloads,pCloudDrive,pCloudLocal}/"*" "/home/ravas.old/*")
sudo rsync -aAXv / "${homearray[@]/#/--exclude=}" --exclude={"/dev/*","/proc/*","/sys/*","/tmp/*","/run/*","/mnt/*","/media/*","/lost+found"} /run/media/ravas/system_backup

说明:"${homearray[@]/#/--exclude=}"做三件事;首先,[@]使其扩展到数组的所有元素;然后/#/--exclude=用"--exclude ="替换每个元素的开头(实际上,它在每个元素前加上"--exclude ="),最后用双引号将通配符("* "),以免造成破坏.

Explanation: "${homearray[@]/#/--exclude=}" does three things; first, the [@] makes it expand to all of the elements of the array; then the /#/--exclude= "replaces" the beginning of each element with "--exclude=" (essentially, it prepends "--exclude=" to each element), and finally the double-quotes around it keep the wildcards ("*") from expanding and wreaking havoc.

这篇关于Bash:需要帮助,将变量传递给rsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆