Bash array

Time:2020-3-23

There are two types of arrays:

  • Index array
  • Associate array (HashMap in Java), associate value with key

Declare array:

#Declare index array
declare -a ary
#Declare associative array
declare -a ary

Reference array: ${ary [IDX]}

Braces are required

To access the array name directly is to access the first element

[[email protected] ~]# ani[0]=dog
[[email protected] ~]# ani[1]=cat
[[email protected] ~]# echo ani
ani
[[email protected] ~]# echo ani[0]
ani[0]
[[email protected] ~]# echo $ani[0]
dog[0]
[[email protected] ~]# echo ${ani[0]}
dog
[[email protected] ~]# echo ${ani[1]}
cat

Assign value to array

Read read array

Only option – A is to read the array

# read -a li
a b 1 d a2
[[email protected] ~]# echo $li
a
[[email protected] ~]# echo ${li[0]}
a
[[email protected] ~]# echo ${li[2]}
1
[[email protected] ~]# echo ${li[4]}
a2

Get the number of array elements

  • ${#ary[*]}
  • ${#ary[@]}

Gets the length of an element in an array

${Chen ary [indx / name]}

Get all elements of the array

  • ${ary[*]}
  • ${ary[@]}

Associative array

It is better to declare the associative array before use, and use the – a option.

[[email protected] scripts]# declare -A map
[[email protected] scripts]# map=([aa]=df12 [dd]=132432 [2]=12d)
[[email protected] scripts]# echo ${map[*]}
132432 df12 12d
[[email protected] scripts]# echo ${map[aa]}
df12
[[email protected] scripts]# echo ${map[2]}
12d

[[email protected] ~]# declare -A l3
[[email protected] ~]# l3[a]=aa
[[email protected] ~]# l3[b]=bb
[[email protected] ~]# echo ${l3[a]}
aa
[[email protected] ~]# echo ${l3[b]}
bb
[[email protected] ~]# echo ${#l3[*]}
2
[[email protected] ~]# echo ${#l3[@]}
2
[[email protected] ~]# echo ${#l3[a]}
2
[[email protected] ~]# echo ${#l3[b]}
2
[[email protected] ~]# l3[c]=1dsfs
[[email protected] ~]# echo ${#l3[c]}
5
[[email protected] ~]# echo ${l3[*]}
aa bb 1dsfs
[[email protected] ~]# echo ${l3[@]}
aa bb 1dsfs

Slice of array

${ary[@]:offset:length}

  • Offset: cut from the first
  • Length: how long to cut
[[email protected] scripts]# l1=(1 2 3 4 5 6)
[[email protected] scripts]# echo ${l1[@]:2:2}
3 4

Delete an element in an array

# l1=(1 2 3 4 5 6)
[[email protected] scripts]# echo ${l1[@]:2:2}
3 4
[[email protected] scripts]# unset l1[1]
[[email protected] scripts]# echo ${l1[@]}
1 3 4 5 6

Practice

1. Generate 10 random integers to get the maximum value

$random is a built-in variable that can generate random integers.

#!/bin/bash
#

declare -a rand
declare -i max=0

for i in {0..9}; do
    rand[$i]=$RANDOM
    echo ${rand[$i]}
    if [ $max -lt ${rand[$i]} ]; then
        max=${rand[$i]}
    fi

done
echo "max is $max"

2. Count the total number of lines in the / var / log / *. Log file.

Key point: files = (/ var / log / *. Log). In brackets is the globbing style, which will automatically expand.

#!/bin/bash
#

declare -a files
declare -i lines

files=(/var/log/*.log)

for ((i=0;i

QQ group of mutual learning in C / C + +: 877684253

Bash array

My wechat: xiaoshitou5854

Recommended Today

PHP JavaScript syntax comparison, quick check

PHP, JavaScript syntax comparison, quick check From the view of full stack engineers, they have learned more computer languages and often confuse the functions of different languages. As a full stack PHPer, often PHP, JavaScript syntax silly division is not clear, Baidu, look up the manual to network speed. It’s better to collect this article, […]