簡體   English   中英

如何在bash shell中使用節讀取配置文件

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

我在部分中有這樣的配置文件

[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/

我不確定如何讀取部分配置文件然后在命令行上使用

要使用--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可以從stdin中為-輸入讀取排除模式,因此它更短:

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排除空行(這是可選的)

以上所有內容均從配置中提取相關部分。

如果您的配置文件位於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-filter這些規則應用於該目錄及其子目錄中的文件。 這是保留和組織rsync規則的有效方法。

同樣,rsync讀取包含和排除規則的順序也很重要。 使用這些過濾器文件,您可以控制該順序。 當您嘗試使rsync規則正常工作時,這是一個重要的優勢。

我承認,我對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的組參數,您可以登錄操作到不同的文件,例如。

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