Shell simulation Two-dimensional array parsing configuration file

Source: Internet
Author: User

The first few days of the project team out of the shell OJ questions to practice, the topic is about:

The existing configuration file Conf.ini as follows, write the shell, enter title and key, output its value,

If input FIFO1 a1, then output 11

file [Fifo1]a1= b1= c1= [fifo2]a2= b2=] c2=   [Fifo3]a3= b3= c3= 

Just because these days to learn the use of arrays, so using the shell to simulate a two-dimensional array, now blog, to treat students, a total of discussion.

Note: This solution does not consider performance, implementation techniques, etc., only in the experimental simulation of two-dimensional arrays.

#!/bin/Bashput_array () {CatConf.ini |Egrep-V"^[[:blank:]]*#|^[[:blank:]]*$"|sed 's/#.*//g'>file_$$ whileRead Line Do        if Echo$line |grep-qs"\["; Thentitle=$(Echo$line |grep-op'(? <=\[) \w* (? =\])')        Elsetypeset integer Index=0Key_val=($(Echo$line |Xargs-D =))             forIinch${key_val[*]}             DoLet index=index+1                if[ $(Echo "$index%2"|BC)-ne0]; ThenKey=$iElsevalue=$i result[$title, $key]=$valuefi             Done         fi         Done<file_$$RMFile_$$}get_array () {In_title=$1In_key=$2     forIinch${!result[*]}     Do["$i" = "$in _title, $in _key"] && echo ${result[$i]} Done}if[$#-ne2]; Then    Echo "Input num Error"Exit1fitypeset-A Resultput_arrayget_array $1$2

Shell simulation Two-dimensional array parsing configuration file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.