简体   繁体   English

如何在bash shell中使用节读取配置文件

[英]How to read config files with section in bash shell

I have the configuration file like this in sections 我在部分中有这样的配置文件

[rsync_includes]
user
data
conf


[rsync_exclude]
tmp
.pyc
*/vendor


[javascript]
utils
data

I have the patterns which i want to exlude in rsync and other configuration data in that file 我有要在该文件中的rsync和其他配置数据中排除的模式

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 我不确定如何读取部分配置文件然后在命令行上使用

To use --exclude-from you will have to isolate the relevant section of the config into a temporary file. 要使用--exclude-from您必须将配置的相关部分隔离到一个临时文件中。 This is easy to do with a bit of sed: 使用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. 我为了清楚起见省略了错误检查和清理。

Note that rsync can read the exclude pattern from the stdin for an - input, so this is even shorter: 请注意,rsync可以从stdin中为-输入读取排除模式,因此它更短:

sed -n '1,/rsync_exclude/d;/\[/,$d;/^$/d;p' config.file | \
  rsync -avz --exclude-from - source/ destination/

Explanation 说明

  • The 1,/rsync_exclude/d excludes all lines up to the rsync_exclude section entry 1,/rsync_exclude/d排除直到rsync_exclude节条目的所有行
  • The /\\[/,$d excludes everything from the start of the next section to the end of the file /\\[/,$d排除从下一节开始到文件末尾的所有内容
  • The /^$/d excludes empty lines (this is optional) /^$/d排除空行(这是可选的)

All of the above extracts the relevant section from the config. 以上所有内容均从配置中提取相关部分。

If your configuration file is in config.ini , then run a bash script: 如果您的配置文件位于config.ini ,请运行bash脚本:

rm rsync-filter
while IFS= read -r line
do
    case "$line" in
        \[rsync_includes\])  command=include ;;
        \[rsync_exclude\]) command=exclude ;;
        \[*) command= ;;
        *) [ "$command"  -a "$line" ] && echo "$command $line" >>rsync-filter
    esac
done <config.ini

After that runs, it creates rsync-filter which contains both the include and exclude rules and can be used with rsync as: 运行之后,它将创建rsync-filter,其中包含包含和排除规则,并且可以与rsync一起使用,如下所示:

rsync -avz --filter='merge rsync-filter' source/ destination/

Separately, rsync offers the -F option which is equivalent to --filter='dir-merge /.rsync-filter' . 另外, rsync提供-F选项,它等效于--filter='dir-merge /.rsync-filter' This loads include/exclude rules from the file /source/.rsync-filter and, further, as rsync goes deeper into the directory tree, it will look for and load rules from .rsync-filter files that it finds and apply those rules to files in that directory and its subdirectories. 此加载从文件/source/.rsync-filter包含/排除规则,此外,随着rsync深入目录树,它将从.rsync-filter文件中查找并加载规则, .rsync-filter这些规则应用于该目录及其子目录中的文件。 This is a powerful way to keep and organize rsync rules. 这是保留和组织rsync规则的有效方法。

Also, the order in which rsync reads include and exclude rules is important. 同样,rsync读取包含和排除规则的顺序也很重要。 With these filter files, you retain control over that order. 使用这些过滤器文件,您可以控制该顺序。 That is an important advantage when you are trying to get rsync rules to work right. 当您尝试使rsync规则正常工作时,这是一个重要的优势。

I will admit that I'm not familiar with rsync, but I would format that data differently, myself. 我承认,我对rsync不熟悉,但我自己会对数据进行格式化。

# rsync-data-file+.txt

rsync-includes:user
rsync-includes:data
rsync-includes:conf

rsync-exclude:tmp
rsync-exclude:.pyc
rsync-exclude:\*\/vendor

javascript:utils
javascript:data

From there, you can do the following:- 从那里,您可以执行以下操作:

#!/usr/bin/env bash
set -x

while read line
do
    if [ $(echo "${line}" | sed -n '/rsync-includes/'p) ]
    then
    parameter=$(echo "${line}" | cut -d':' -f2)
    rsync "${parameter}" (other switches here etc)
fi
done < rsync-data-file+.txt

This way you can customise your command line depending on which group the parameter belongs to; 这样,您可以根据参数所属的组来自定义命令行。 so with parameters from the javascript group, you can log the operations to a different file, for instance. 所以从JavaScript的组参数,您可以登录操作到不同的文件,例如。

#!/bin/sh

typeset -A Nconfig # init array

typeset -A Oconfig # init array , u can declare multiple array for each section.s

while read line
do
    if [ "$line" = "[SECTION1]" ]
    then
        SECTION1=1
        SECTION2=0
        continue
    fi
    if [ "$line" = "[SECTION2]" ]
        then
        SECTION1=0
        SECTION2=1
        continue
        fi
    if [ "$line" = "[SECTION3]" ]
        then
        SECTION1=0
        SECTION2=0
        continue
        fi



    if [ $SECTION1= 1 ]
    then
        if echo $line | grep -F = &>/dev/null
            then
            varname=$(echo "$line" | cut -d '=' -f 1)
            echo "Novar $varname"
            Nconfig[$varname]=$(echo "$line" | cut -d '=' -f 2)
        fi
    fi
    if [ $SECTION2 = 1 ]
    then
        if echo $line | grep -F = &>/dev/null
            then
            varname=$(echo "$line" | cut -d '=' -f 1)
            Oconfig[$varname]=$(echo "$line" | cut -d '=' -f 2)
        fi
    fi


   done < Config

echo "SECTION1 FROM=${Nconfig[FROM]}"
echo "SECTION2FROM=${Oconfig[FROM]}"



[SECTION1]
FROM=abc@pqr.com
TO=abc@pqr.com
SIZE=80
THRESHOULD=60
[SECTION2]
FROM=xxxx@pqr.com
TO=xxxx@pqr.com,yyyy@pqr.com
SIZE=60
THRESHOULD=30
[SECTION3]
FROM=AAAA@pqr.com
TO=BBBB@pqr.com,yyyy@pqr.com
SIZE=60
THRESHOULD=30
LOCATION=/mnt/device/user1/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM