如何在bash shell中使用节读取配置文件
我在部分中有这样的配置文件
I have the configuration file like this in sections
[rsync_includes]
user
data
conf
[rsync_exclude]
tmp
.pyc
*/vendor
[javascript]
utils
data
我具有要在该文件中的rsync和其他配置数据中排除的模式
I have the patterns which i want to exlude in rsync and other configuration data in that file
现在我很困惑如何在命令行上使用这些模式
Now i am confused how can i use those patterns on command line
rsync -avz --exclude-from 'content from config file rsync exclude' source/ destination/
我不确定如何读取配置文件的一部分然后在命令行上使用
I am not sure how can read part of config file and then use on command line
要使用-exclude-from
,您必须将配置的相关部分隔离到一个临时文件中.只需使用sed即可轻松做到这一点:
To use --exclude-from
you will have to isolate the relevant section of the config into a temporary file. This is easy to do with a bit of sed:
tmp_file=$(mktemp)
sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file > $tmp_file
rsync -avz --exclude-from $tmp_file source/ destination/
为了清晰起见,我省略了错误检查和清理.
I am omitting error checking and cleanup for clarity.
请注意,rsync可以从标准输入中读取-
输入的排除模式,因此它更短:
Note that rsync can read the exclude pattern from the stdin for an -
input, so this is even shorter:
sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file | \
rsync -avz --exclude-from - source/ destination/
说明
-
1,//rsync_exclude/d
排除直到rsync_exclude节条目的所有行 -
/\ [/,$ d
排除了从下一部分的开始到文件末尾的所有内容 -
/^ $/d
排除空行(这是可选的)
- The
1,/rsync_exclude/d
excludes all lines up to the rsync_exclude section entry - The
/\[/,$d
excludes everything from the start of the next section to the end of the file - The
/^$/d
excludes empty lines (this is optional)
以上所有内容均从配置中提取相关部分.
All of the above extracts the relevant section from the config.