Several methods of shell traversing array

Time:2021-9-14

background

One day I encountered a problem. The phenomenon is as follows: the first kind of traversal will print normal elements, but the second kind of traversal will print only one element!

#!/bin/bash
#$1 for file directory
#Execution eg:
#  sh your_shell.sh /user/lib/
 
Echo "start time: ` date '+% Y -% m -% d% H:% m:% s' `"
#1. Get all files
files=`ls $1`
 
#2. Define an array variable
declare -a array_files01
 
 
#Array index is initialized to 0
m1=0
 
 
#Traversal
for ays in ${files}; do
{
 #Get array length
 num_1=${#array_files01[@]}
 
 #To add elements to the array, index needs + 1 each time
 array_files01[m1]=${ays};
 m1=`expr ${m1} + 1`;
 
 fi
}
done
 
Echo "end allocating array time: ` date '+% Y -% m -% d% H:% m:% s' `"
#Print prompt information
Echo "1 array length: ${#array_files01 [@]}"
 
#First traversal: normal
for i in ${array_files01[@]}; do
 echo $i
done 
 
#Second traversal: abnormal
for i in ${array_files01}; do
 echo $i
done

Several methods of traversing arrays

1. For loop

###################First kind#######################
for i in ${array[@]}
#It can also be written as for element in ${array [*]}
do
echo $i
done

##################Second########################
for i in ${array[*]}
do
echo $i ;
done
 
##################Third########################
for(( i=0;i<${#array[@]};i++)) do
#${#array [@]} get array length for loop
echo ${array[i]};
done;

2. While loop

i=0 
while [ $i -lt ${#array[@]} ] 
#Enter the loop body when the variable (subscript) is less than the length of the array
do 
 echo ${ array[$i] } 
 #Print array elements by subscript
 let i++ 
done

In addition to the subscript problem, it should also be noted that @ and * are not completely equivalent, and the resolution when surrounded by double quotes is sometimes slightly different.

  • For traversal, @ in ${#arr1 [@]} can be interchanged with * and is equivalent when surrounded by double quotes, “${#arr1 [@]}”.
  • For… In traversal. When directly traversing array elements, ${Arr1 [@]} is not surrounded by double quotation marks, @ is equivalent to *, but when parsing array elements, the contents of the elements will also be separated by spaces, resulting in parsing errors. When ${Arr1 [*]} is surrounded by double quotation marks, all elements in the array will be separated by spaces and treated as one element, and the parsing is also wrong. Only when ${Arr1 [@]} is surrounded by double quotes can array elements be parsed correctly, so “${Arr1 [@]}” must be used when directly traversing array elements. When traversing array elements by traversing subscripts, ${! Arr1 [*]} is surrounded by double quotation marks, all element subscripts will be separated by commas to form a string, so the subscript set cannot be obtained in this way. The subscripts can be obtained correctly in the other three cases.
  • While traversal, not affected by @, * and double quotes.

This is the end of this article about several methods of shell traversing arrays. For more information about shell traversing arrays, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!