2011-05-12 265 views
12

我正在尋找類似Pythons的ConfigParser或Perl的Config :: INI的shell腳本。我已經在過去提供了文件來完成這個任務,但我寧願閱讀而不是執行我的「配置文件」。有沒有人知道任何與上述可用於shell(或bash)腳本的模塊相當的東西?從shell腳本讀取配置文件

感謝, 傑裏

+1

可能重複[如何在shell腳本中獲取INI值?](http://stackoverflow.com/questions/6318809/how-do-i-grab-an-ini-value-within-a -shell-script) – kenorb 2015-03-01 15:24:07

回答

12

你不希望它來源,所以你應該:

。讀的配置,2.verify線3.eval他們

CONFIGFILE="/path/to/config" 
echo "=$ADMIN= =$TODO= =$FILE=" #these variables are not defined here 
eval $(sed '/:/!d;/^ *#/d;s/:/ /;' < "$CONFIGFILE" | while read -r key val 
do 
    #verify here 
    #... 
    str="$key='$val'" 
    echo "$str" 
done) 
echo =$ADMIN= =$TODO= =$FILE= #here are defined 

樣本的配置文件

ADMIN: root 
TODO: delete 

var=badly_formtatted_line_without_colon 

#comment 
FILE: /path/to/file 

如果你運行上面的示例應該得到(未測試):

== == == 
=root= =delete= =/path/to/file= 

肯定這不是最好的解決方案 - 也許有人發佈更好的解決方案。

+0

我想我正在考慮像ConfigParser這樣的預先包裝的東西,但是這當然有效。謝謝! – zenzic 2011-05-12 21:50:54

+0

在配置文件中處理空白行: ''當讀取鍵值時,eval $(sed'/^*#/ d; s /:/ /;'<「$ CONFIGFILE」| val if [-n「$ {key }「];然後 str =」$ key ='$ val'「 echo」$ str「 fi done)' – slonik 2014-07-17 08:47:16

+0

@slonik處理空行的好處。在腳本中,'sed'過濾掉不需要的行,這樣就可以添加另一個命令,例如:'/^* $/d'(例如刪除只包含任意數量空格的行 - 例如零個或多個)。謝謝你指點我的問題。編輯我的答案。 – jm666 2014-07-17 09:16:56

3
#!/bin/bash 
# Author: CJ 
# Date..: 01/03/2013 

## sample INI file save below to a file, replace "^I" with tab 
#^I [ SECTION ONE  ] 
#TOKEN_TWO^I ="Value1 two " 
#TOKEN_ONE=Value1 One 
#TOKEN_THREE=^I"Value1^I three" # a comment string 
#TOKEN_FOUR^I=^I"^IValue1 four" 
# 
#[SECTION_TWO] 
#TOKEN_ONE=Value1 One ^I^I^I# another comment string 
#TOKEN_TWO^I ="Value1 two " 
#TOKEN_THREE=^I"Value1^I three" 
#TOKEN_FOUR^I=^I"^IValue1 four" 
## sample INI file 

export INI= # allows access to the parsed INI values in toto by children 
iniParse() { 
    # Make word separator Linefeed(\n) 
    OIFS="${IFS}" 
    IFS=$(echo) 

    SECTION=_ 
    while read LINE; do { 
     IFS="${OIFS}" 

     # Skip blank lines 
     TMP="$(echo "${LINE}"|sed -e "s/^[ \t]*//")" 
     if [ 0 -ne ${#TMP} ]; then 
      # Ignore comment lines 
      if [ '#' == "${LINE:0:1}" -o '*' == "${LINE:0:1}" ]; then 
       continue 
      fi # if [ '#' == "${LINE:0:1}" -o '*' == "${LINE:0:1}" ]; then 

      # Section label 
      if [ "[" == "${LINE:0:1}" ]; then 
       LINE="${LINE/[/}" 
       LINE="${LINE/]/}" 
       LINE="${LINE/ /_}" 
       SECTION=$(echo "${LINE}")_ 
      else 
       LINE="$(echo "${LINE}"|sed -e "s/^[ \t]*//")" 
       LINE="$(echo "${LINE}"|cut -d# -f1)" 

       TOKEN="$(echo "${LINE:0}"|cut -d= -f1)" 
       EQOFS=${#TOKEN} 
       TOKEN="$(echo "${TOKEN}"|sed -e "s/[ \t]*//g")" 

       VALUE="${LINE:${EQOFS}}" 
       VALUE="$(echo "${VALUE}"|sed -e "s/^[ \t=]*//")" 
       VALUE="$(echo "${VALUE}"|sed -e "s/[ \t]*$//")" 

       if [ "${VALUE:0:1}" == '"' ]; then 
        echo -n "${SECTION}${TOKEN}=${VALUE}" 
        echo -e "\r" 
       else 
        echo -n "${SECTION}${TOKEN}="\"${VALUE}\""" 
        echo -e "\r" 
       fi # if [ "${VALUE:0:1}" == '"' ]; then 
      fi # if [ "[" == "${LINE:0:1}" ]; then 
     fi # if [ 0 -ne ${#TMP} ]; then 

     IFS=$(echo) 
    } done <<< "$1" 

    IFS="${OIFS}" # restore original IFS value 
} # iniParse() 

# call this function with the INI filespec 
iniReader() { 
    if [ -z "$1" ]; then return 1; fi 

    TMPINI="$(<$1)" 
    TMPINI="$(echo "${TMPINI}"|sed -e "s/\r//g")" 
    TMPINI="$(echo "${TMPINI}"|sed -e "s/[ \t]*\[[ \t]*/[/g")" 
    TMPINI="$(echo "${TMPINI}"|sed -e "s/[ \t]*\][ \t]*/]/g")" 

    INI=`iniParse "${TMPINI}"` 
    INI="$(echo "${INI}"|sed -e "s/\r/\n/g")" 
    eval "${INI}" 

    return 0 
} # iniReader() { 

# sample usage 
if iniReader $1 ; then 
    echo INI read, exit_code $? # exit_code == 0 
    cat <<< "${INI}" 
    cat <<< "${SECTION_ONE_TOKEN_FOUR}" 
    cat <<< "${SECTION_ONE_TOKEN_THREE}" 
    cat <<< "${SECTION_TWO_TOKEN_TWO}" 
    cat <<< "${SECTION_TWO_TOKEN_ONE}" 
else 
    echo usage: $0 filename.ini 
fi # if iniReader $1 ; then 
3

你可能想看看cfget可與sudo apt-get install cfget安裝。

+0

如果您有權將它安裝在您的系統上,那麼這是一個非常有用的工具。 – 2014-07-30 14:52:48