shell 類比二維數組解析設定檔

來源:互聯網
上載者:User

標籤:

前幾日項目組內出shell OJ題進行練習, 題目大概為:

現有設定檔conf.ini如下,編寫shell,輸入title和key,輸出其值,

如輸入FIFO1 a1 ,則輸出11

#this is a config file[FIFO1]a1=11 b1=12 c1=13[FIFO2]a2=21 b2=22 c2=23[FIFO3]a3=31 b3=32 c3=33

 

恰因這幾日內在學習數組的用法,故使用shell來類比二維數組,現部落格之,以饗同學,共研討。

注:本解法未考慮效能,實現技巧等,只在於實驗類比二維數組。

#!/bin/bashput_array(){    cat conf.ini | egrep -v "^[[:blank:]]*#|^[[:blank:]]*$" |sed ‘s/#.*//g‘  >file_$$    while read line    do        if echo $line | grep -qs "\[" ;then            title=$(echo $line | grep -oP ‘(?<=\[)\w*(?=\])‘)        else            typeset integer index=0            key_val=($(echo $line|xargs -d =))            for i in ${key_val[*]}            do                let index=index+1                if [ $(echo "$index%2"|bc) -ne 0 ];then                     key=$i                else                     value=$i                    result[$title,$key]=$value                fi            done         fi        done  <file_$$    rm file_$$}get_array(){    in_title=$1    in_key=$2    for i in ${!result[*]}    do        [ "$i" == "$in_title,$in_key" ] && echo ${result[$i]}    done}if [ $# -ne 2 ];then    echo "input num error"    exit 1fitypeset -A resultput_arrayget_array $1 $2

 

shell 類比二維數組解析設定檔

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.