bash 如何在bash shell中读取带有部分的配置文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21347695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:22:00  来源:igfitidea点击:

How to read config files with section in bash shell

linuxbashshellsed

提问by user3147180

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

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

采纳答案by mockinterface

To use --exclude-fromyou will have to isolate the relevant section of the config into a temporary file. This is easy to do with a bit of sed:

要使用,--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/

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/dexcludes all lines up to the rsync_exclude section entry
  • The /\[/,$dexcludes everything from the start of the next section to the end of the file
  • The /^$/dexcludes empty lines (this is optional)
  • 1,/rsync_exclude/d排除了所有行到rsync_exclude部分进入
  • /\[/,$d排除一切从下一章节的开始到文件的末尾
  • /^$/d不包括空行(这是可选的)

All of the above extracts the relevant section from the config.

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

回答by petrus4

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 组的参数,您可以将操作记录到不同的文件中。

回答by Sagar

#!/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]
[email protected]
[email protected]
SIZE=80
THRESHOULD=60
[SECTION2]
[email protected]
[email protected],[email protected]
SIZE=60
THRESHOULD=30
[SECTION3]
[email protected]
[email protected],[email protected]
SIZE=60
THRESHOULD=30
LOCATION=/mnt/device/user1/

回答by John1024

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, rsyncoffers the -Foption which is equivalent to --filter='dir-merge /.rsync-filter'. This loads include/exclude rules from the file /source/.rsync-filterand, further, as rsyncgoes deeper into the directory tree, it will look for and load rules from .rsync-filterfiles that it finds and apply those rules to files in that directory and its subdirectories. This is a powerful way to keep and organize rsync rules.

另外,rsync提供-F相当于的选项--filter='dir-merge /.rsync-filter'。这会从文件中加载包含/排除规则,/source/.rsync-filter并且随着rsync深入目录树,它将从.rsync-filter它找到的文件中查找和加载规则,并将这些规则应用于该目录及其子目录中的文件。这是一种保留和组织 rsync 规则的强大方法。

Also, the order in which rsync reads include and exclude rules is important. 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 读取包含和排除规则的顺序也很重要。使用这些过滤器文件,您可以保留对该顺序的控制。当您尝试使 rsync 规则正常工作时,这是一个重要的优势。