bash - Creating Arrays From Unknown Number of External Lists -
this question related importing values text file (similar 1 of previous ones), added complexity (the more learn bash scripting more challenging becomes)
the goal: create array of day_.... on each outer loop iteration. i'm trying assuming no knowledge of how many day_... lists exist in *.txt file.
the issue: @ moment inner loop iterates once (should iterate number of elements on monday
. and, also, i'm using my_sub_dom=$( sed 's/=.*//' weekly.txt )
number of lists/arrays in weekly.txt , filter ones contain day
.
bash script:
#!/bin/bash source weekly.txt declare -a my_sub_dom day=( ${monday[*]} ) my_sub_dom=$( sed 's/=.*//' weekly.txt ) # construct list of number of of lists in text file #echo "${my_sub_dom}" counter=0 main_counter=0 in "${day[@]}" let main_counter=main_counter+1 j in "${my_sub_dom[@]}" # echo "$j" if grep -q "day" "${my_sub_dom}" echo "$j" sub_array_name="${my_sub_dom[counter]}" # storing list name sub_array_content=( ${sub_array_name[*]} ) echo "${sub_array_content}" else echo "no" fi let counter=counter+1 done echo "$counter" counter=0
done echo "$main_counter"
text file format:
day_mon=( "google" "yahoo" "amazon" ) day_tu=( "cnn" "msnbc" "google" ) day_wed=( "nytimes" "fidelity" "stackoverflow" ) monday= ( "one" "two" "three" ) ....
script output:
grep: day_mon day_tu day_wed monday: no such file or directory no 1 grep: day_mon day_tu day_wed monday: no such file or directory no 1 grep: day_mon day_tu day_wed monday: no such file or directory no 1 3
please let me know if you'd other information.... , appreciate input in matter, i've been trying couple of days now.
thank you
given file weekly.txt
containing
day_mon=( "google" "yahoo" "amazon" ) day_tu=( "cnn" "msnbc" "google" ) day_wed=( "nytimes" "fidelity" "stackoverflow" ) monday=( "one" "two" "three" )
you can loop through each array named day_
-something with
#!/bin/bash # set arrays defined in file source weekly.txt # variables prefixed "day_" (bash 4) name in "${!day_@}" echo "the contents of array $name is: " # use indirection expand array arrayexpansion="$name[@]" value in "${!arrayexpansion}" echo "-- $value" done echo done
this results in:
the contents of array day_mon is: -- google -- yahoo -- amazon contents of array day_tu is: -- cnn -- msnbc -- google contents of array day_wed is: -- nytimes -- fidelity -- stackoverflow
Comments
Post a Comment