Multi Dimensional array in bash

Hi, I'm developing a script which contains a multi dimensional array, however for some reason the array is not iterating.

When executing the script, services are listed as arguments from argument 2. Ex voice data sms.

service=${@:2};

for services in $service
do

data_call_type=CALL_TYPE;
data_term=DATA;
voice_call_type=CALL_TYPE;
voice_term=VOICE-MO;
sms_call_type=CALL_TYPE;
sms_term=SMS-MO;
fi


done

path_dir=$TMPDIR/

for services in $service
    do
			declare -a arr_cust=("A" "B" "C" "D" "E" "F" "G" "H" "I" "J")
			declare -a arr_ids=("124" "94" "61" "85" "95" "105" "106" "107" "134" "158")
			
			for index in ${!array[*]};
			do
			
            charged_data=`grep -s $data_call_type=$data_term $path_dir/* | grep -s ACS_CUST_ID=${arr_ids[$index]} | wc -l`
 notcharged_data=`grep -s $data_call_type=$data_term $path_dir/* | grep -s ACS_CUST_ID={arr_ids[$index]} | wc -l`


eval charged="\$charged_$services"
        eval notcharged="\$notcharged_$services"
        
echo ${services}_charged $charged > $OUTPUTDIR/$(basename $0 .sh).$services.${arr_cust[$index]}

The issue i'm finding is that the grep is not working properly :

grep -s ACS_CUST_ID=${arr_ids[$index]}

also I am noticing that array is not iterating over index of both arrays. what I am after is that the each index perform all greps for charged_data and notcharged_data and output to file with extension listing the value of the index from array 'arr_cust'.

Can someone provide some help where i'm wrong in the above?

---------- Post updated at 12:06 PM ---------- Previous update was at 12:05 PM ----------

Kindly ignore the fact that for loop is doubled. This is because there are additional if conditions in the script

is this not already being discussed here?: https://www.unix.com/shell-programming-and-scripting/276798-multi-dimensional-array.html#post303012300

Robin

Just to get a common understanding of some terms and some statements in your script

  • I can't see a multidimensional array. What would you call a multidim array? Why do you need it?
  • The arr_cust is used ONLY for the output file name - is that intended?
  • Both charged_data and notcharged_data are populated by exactly the same algorithm (except the $ sign is missing for the latter) - intended?
  • Both arr_cust and arr_ids are constants - why redefined in every loop iteration?
  • Same for the data_call et al. in the services loop.
  • No surprise the index loop doesn't work as it loops across the undefined array .
  • Some done s missing for several for loops.
  • What do you need the (deprecated, dangerous) eval commands for? Can't the result be achieved in another way?

All this is quite irritating and misleading. Why don't you post a decent input sample, the desired output for it, and describe the logics connecting the two in plain English?

1 Like