I created an array of a sequence of numbers. How can I check the size of each element, and depending on the number of digits, each element has I want to add more numbers to it. Below I have some code that accomplishes this using one variable, but I want to do it with the array
#array I would like to use
start=0
end=20
myarray=( $(seq $start $end) )
#example of how I accomplish this using a variable. checks if the number of integers is between 1-5
and adds some numbers to the end. I would like to accomplish this but with an array.
number=2
local newnum=`expr length "$number"` #get lenght of the variable
if [ "$newnum" -eq "1" ];then number="0000${number}"
elif [ "$newnum" -eq "2" ];then number="000${number}"
elif [ "$newnum" -eq "3" ];then number="00${number}"
elif [ "$newnum" -eq "4" ];then number="0${number}"
elif [ "$newnum" -eq "5" ]; then echo "you enter five numbers" #number remains the size
else exit
fi #end if 0 1 Answer
One way:
for val in "${myarray[@]}"
do len=${#val} echo $val , $len ....do action depending on $len .....
done 4