{"id":1360,"date":"2019-06-17T11:03:10","date_gmt":"2019-06-17T16:03:10","guid":{"rendered":"http:\/\/blog.ls-al.com\/?p=1360"},"modified":"2019-06-17T11:03:11","modified_gmt":"2019-06-17T16:03:11","slug":"bash-read-array-from-config-file","status":"publish","type":"post","link":"https:\/\/blog.ls-al.com\/bash-read-array-from-config-file\/","title":{"rendered":"Bash Read Array From Config File"},"content":{"rendered":"\n
I was recently needing to read values from a configuration file into bash and had some success with reading json with jq into bash array(s). However I resorted to a non json version which worked well. Something like this.<\/p>\n\n\n\n
Config File<\/strong><\/p>\n\n\n Code <\/strong><\/p>\n\n\n Run<\/p>\n\n\n I was recently needing to read values from a configuration file into bash and had some success with reading json<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1360","post","type-post","status-publish","format-standard","hentry","category-bash"],"_links":{"self":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/comments?post=1360"}],"version-history":[{"count":0,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/posts\/1360\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/media?parent=1360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/categories?post=1360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ls-al.com\/wp-json\/wp\/v2\/tags?post=1360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}\n$cat array-simple.cfg\n[bucket1]\nname=bucket name 1\nexclude=folder1 folder 2\n\n[bucket2]\nname=bucket name 2\nexclude=folder5\n<\/pre><\/div>\n\n\n
\n$ cat array-simple.sh\n#!\/bin\/bash\nwhile read line; do\n if [[ $line =~ ^"["(.+)"]"$ ]]; then\n arrname=${BASH_REMATCH[1]}\n declare -A $arrname\n elif [[ $line =~ ^([_[:alpha:]][_[:alnum:]]*)"="(.*) ]]; then\n declare ${arrname}[${BASH_REMATCH[1]}]="${BASH_REMATCH[2]}"\n fi\ndone < array-simple.cfg\n\necho ${bucket1[name]}\necho ${bucket1[exclude]}\n\necho ${bucket2[name]}\necho ${bucket2[exclude]}\n\nfor i in "${!bucket1[@]}"; do echo "$i => ${bucket1[$i]}"; done\n\nfor i in "${!bucket2[@]}"; do echo "$i => ${bucket2[$i]}"; done\n<\/pre><\/div>\n\n\n
\n$ .\/array-simple.sh \nbucket name 1\nfolder1 folder 2\nbucket name 2\nfolder5\nexclude => folder1 folder 2\nname => bucket name 1\nexclude => folder5\nname => bucket name 2\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"