【问题标题】:How to read config files with section in bash shell如何在 bash shell 中读取带有部分的配置文件
【发布时间】:2014-01-25 06:57:53
【问题描述】:

我在部分中有这样的配置文件

[rsync_includes]
user
data
conf


[rsync_exclude]
tmp
.pyc
*/vendor


[javascript]
utils
data

我有想要在 rsync 和该文件中的其他配置数据中排除的模式

现在我很困惑如何在命令行上使用这些模式

rsync -avz --exclude-from 'content from config file rsync exclude' source/ destination/

我不确定如何读取配置文件的一部分然后在命令行上使用

【问题讨论】:

  • (a) rsync 的 --exclude-from 需要一个文件作为参数,而不是文件的内容。 (b) 存储 rsync 包含+排除的标准文件称为.rsync-filter,您可以使用 -F 选项加载它。

标签: linux bash shell sed


【解决方案1】:

要使用--exclude-from,您必须将配置的相关部分隔离到一个临时文件中。用一点 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/

为了清楚起见,我省略了错误检查和清理。

请注意,rsync 可以从标准输入中读取 - 输入的排除模式,因此它更短:

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 不包括空行(这是可选的)

以上所有内容都从配置中提取相关部分。

【讨论】:

    【解决方案2】:

    如果您的配置文件在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
    

    运行后,它会创建包含包含和排除规则的 rsync-filter,并且可以与 rsync 一起使用:

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

    另外,rsync 提供了-F 选项,它等效于--filter='dir-merge /.rsync-filter'。这会从文件/source/.rsync-filter 中加载包含/排除规则,此外,随着rsync 深入到目录树中,它将从它找到的.rsync-filter 文件中查找并加载规则,并将这些规则应用于其中的文件目录及其子目录。这是保存和组织 rsync 规则的有效方式。

    另外,rsync 读取包含和排除规则的顺序也很重要。使用这些过滤器文件,您可以控制该顺序。当您尝试使 rsync 规则正常工作时,这是一个重要的优势。

    【讨论】:

      【解决方案3】:

      我承认我不熟悉 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
      

      从那里,您可以执行以下操作:-

      #!/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
      

      通过这种方式,您可以根据参数所属的组自定义命令行;例如,使用 javascript 组中的参数,您可以将操作记录到不同的文件中。

      【讨论】:

        【解决方案4】:
        #!/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/
        

        【讨论】:

          猜你喜欢
          • 2010-12-24
          • 2011-04-13
          • 1970-01-01
          • 1970-01-01
          • 2014-10-05
          • 1970-01-01
          • 2011-08-24
          • 2016-01-11
          • 2011-09-18
          相关资源
          最近更新 更多