获取数组元素大小



我创建了一个数字序列数组。我如何检查每个元素的大小,根据数字的数量,每个元素都有。我想给它添加更多的数字。下面我有一些代码可以使用一个变量来实现这一点,但我想用数组来实现

#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 



单向:

for val in "${myarray[@]}"
do
len=${#val}
echo $val , $len
....do action depending on $len .....
done

最新更新