I need to have an array for each "section" in a file containing:
[array0] value1=asdf value2=jkl [array1] value1=1234 value2=5678
I want to get these values ββas follows:
echo ${array0[value1]} echo ${array0[value2]} echo ${array1[value1]} echo ${array1[value2]}
Any thoughts on how to do this? (Explanations will be a bonus)
I already read these underders, but nobody does exactly what I want to do.
Read configuration file in BASH without using "source"
BASH Parsing variables from a configuration file
Array as data structure in BASH (configuration file)?
with bash v4, using associative arrays, save the properties from the configuration file as actual bash variables:
$ while read line; do if [[ $line =~ ^"["(.+)"]"$ ]]; then arrname=${BASH_REMATCH[1]} declare -A $arrname elif [[ $line =~ ^([_[:alpha:]][_[:alnum:]]*)"="(.*) ]]; then declare ${arrname}[${BASH_REMATCH[1]}]="${BASH_REMATCH[2]}" fi done < config.conf $ echo ${array0[value1]} asdf $ echo ${array1[value2]} 5678 $ for i in "${!array0[@]}"; do echo "$i => ${array0[$i]}"; done value1 => asdf value2 => jkl $ for i in "${!array1[@]}"; do echo "$i => ${array1[$i]}"; done value1 => 1234 value2 => 5678
eval -, 100% Bash :
eval
#!/bin/bash die() { printf >&2 "%s\n" "$@" exit 1 } aryname='' linenb=0 while read line; do ((++linenb)) if [[ $line =~ ^[[:space:]]*$ ]]; then continue elif [[ $line =~ ^\[([[:alpha:]][[:alnum:]]*)\]$ ]]; then aryname=${BASH_REMATCH[1]} declare -A $aryname elif [[ $line =~ ^([^=]+)=(.*)$ ]]; then [[ -n aryname ]] || die "*** Error line $linenb: no array name defined" printf -v ${aryname}["${BASH_REMATCH[1]}"] "%s" "${BASH_REMATCH[2]}" else die "*** Error line $linenb: $line" fi done
. , done :
done
done < "filename"
space and funnΕ· sΓΏmbΓ²l=value that will have an equal sign: look = it funny
bash
declare -a <array_name>=(value1 value2 value 3)
echo ${<array_name>[index]}
Edit:
, . , .
,
1.config ( )
100 200 300
2.script ( )
array=() #setup array while IFS=$'\n' read -a config do array+=(${config}) done < file_name #access values echo ${array[0]} echo ${array[1]}
IFS-a , , while.
, , - (), , , - , @anubhava, ...
eval $(gawk -F= '/^\[/{name=gensub(/\[|\]/,"","g");x=0} /=/{print "name[",x++,"]=",$2," "}' config)
, , "[", name gensub(). , "=" , "x" eval .
[
name
gensub()
=
x
Gotta dash - stat -s .
stat -s
Source: https://habr.com/ru/post/1537178/More articles:Java Visualization Preliminary License - javahttps://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1537174/facebook-sharer-returning-500-internal-server-error-on-desktop&usg=ALkJrhgQ4NHSMAysm2jFaB62r3voYLk6hAHow to get url on relative path? - Javascript - javascriptHow to integrate Paypal Payment Gateway on asp.net - c #The average value of the dispersion of the sample and the variance are not close to the average value of the distribution and dispersion - clojureCreate a list of n (strictly positive) values ββso that the list has predefined values ββof x and std. deviation Y - mathCall FORTRAN routine from C # - c #Heroku timezone issue using Moment.js - javascriptPrevent hacking groups of download buttons - cssjava - how to simulate an ECG (electrocardiogram) - javaAll Articles