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