Linux Shell Array Common operation detailed
1 Array Definitions:
Declare-a array Name
Array name = (element 1 element 2 element 3)
1 Declare-a array2 array= (12345)
Arrays are enclosed in parentheses, and the array elements are separated by a space
2 Display Array Length:
Echo ${#array [@]} 5 Echo ${#array [*]}5
Command:
${#数组名 [@ or *]} to get the array length, if the array has no elements, the output is empty
3 Reading array elements:
[@tc_132_227 dm_pid_day]$Echo${array[0]}1[@tc_132_227 dm_pid_day]$Echo${array[1]}2[@tc_132_227 dm_pid_day]$Echo${array[4]}5[@tc_132_227 dm_pid_day]$Echo${array[*]}1 2 3 4 5[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5
Command:
${array name [subscript]}, array subscript starting from 0, and subscript as * or @ symbol, reading the entire array element
4 assigning values to array elements:
[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5[@tc_132_227 dm_pid_day]$ array[1]=9[@tc_132_227 dm_pid_day]$Echo${array[@]}1 9 3 4 5[@tc_132_227 dm_pid_day]$ array[8]=7[@tc_132_227 dm_pid_day]$Echo${array[@]}1 9 3 4 5 7[@tc_132_227 dm_pid_day]$
Command:
Array name [subscript]= element Assignment If the subscript does not exist, the array will automatically add a new element
5 Deleting an array element:
[@tc_132_227 dm_pid_day]$ unset array[0[@tc_132_227 dm_pid_day]$Echo${array[@]}2 3 4 5[@tc_132_227 dm_pid_day]$Echo${#array [*]}4[@tc_132_227 dm_pid_day]$ unset array[1[@tc_132_227 dm_pid_day]$Echo${array[*]}3 4 5[@tc_132_227 dm_pid_day]$Echo${#array [*]}3[@tc_132_227 dm_pid_day]$ unset array[@tc_132_227 dm_pid_day]$Echo${array[*]}[@tc_132_227 dm_pid_day]$
Command:
unset array name [subscript] to delete array element, unset array name to delete entire array
6 Array Shards:
[@tc_132_227 dm_pid_day]$Echo${array[@]}1 2 3 4 5[@tc_132_227 dm_pid_day]$Echo${array[@]:0:1}1[@tc_132_227 dm_pid_day]$Echo${array[@]:1:1}2[@tc_132_227 dm_pid_day]$Echo${array[@]:1:3}2 3 4
[@tc_132_227 dm_pid_day]$ arr= (${array[@]:1:3echo ${arr[@]}2 34echo ${#arr [@]}3[@tc_132_227 dm_pid_day]$
Command:
${array name [@|*]: Start subscript: Length} shards the original array and returns a string with a "space" delimiter, and if you add "()" to the string, you get a new shard array
Linux Shell Array Common operation detailed