Question

array1=$(command1)

echo ${array1[@]}
292102 Mon May 12 22:35:12 2014 289368 Mon May 12 14:18:47 2014 280508 Mon May 12 13:44:12 2014 293976 Mon May 12 12:04:08 2014 293227 Mon May 12 03:07:28 2014 291745 Mon May 12 01:46:23 2014

I need to run loop where I set variable as for the first loop:

${TN}=292102
${LastUpdated}="Mon May 12 22:35:12 2014"

so it should be something like:

${TN}=${array1[0]}
${LastUpdated}="${array1[1]}${array1[2]}${array1[3]}${array1[4]}${array1[5]}"

Number of array members will change after each $(command1)

How can I program some loop that will set variables according to freshly generated array of n members?

EDIT: Just to clarify as my question was marked unclear, I needed to slice the array to set up variables and join some slices together to make up a variable as date value eg. "Mon May 12 22:35:12 2014", I was not able to describe it the proper way then as I haven't heard of array slicing before that.

Was it helpful?

Solution

Using array slicing:

i=0;
while [ $i -lt $((${#array1[@]}/6)) ]; do
     TN=${array1[i*6]}
     LastUpdated="${array1[@]:i*6+1:5}"
     #Do something with TN & LastUpdated here...
     ((i++))
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top