@yanglt7
2018-11-02T06:46:22.000000Z
字数 4150
阅读 834
Shell
1)用小括号将变量值括起来赋值给数组变量,每个变量值之间要用空格进行分隔。
array=(value1 value2 value3 ...)
2)用小括号将变量值括起来,同时采用 key-value 键值对的形式赋值。
array=([1]=one [2]=two [3]=three)
3)通过分别定义数组变量的方法来定义。
array[0]=a;array[1]=b;array[2]=c
4)动态地定义数组变量,并使用命令的输出结果作为数组的内容。
array=($(命令))
或 array=(`命令`)
1)打印数组元素
[root@web001 scripts]# array=(one two three)
[root@web001 scripts]# echo ${array[0]}
one
[root@web001 scripts]# echo ${array[1]}
two
[root@web001 scripts]# echo ${array[2]}
three
[root@web001 scripts]# echo ${array[*]}
one two three
[root@web001 scripts]# echo ${array[@]}
one two three
2)打印数组元素个数
[root@web001 scripts]# echo ${array[*]}
one two three
[root@web001 scripts]# echo ${#array[*]}
3
[root@web001 scripts]# echo ${array[@]}
one two three
[root@web001 scripts]# echo ${#array[@]}
3
3)数组赋值
[root@web001 scripts]# echo ${array[*]}
one two three
[root@web001 scripts]# array[3]=four
[root@web001 scripts]# echo ${array[@]}
one two three four
[root@web001 scripts]# array[0]=ylt
[root@web001 scripts]# echo ${array[*]}
ylt two three four
4)数组的删除
[root@web001 scripts]# echo ${array[*]}
ylt two three four
[root@web001 scripts]# unset array[1]
[root@web001 scripts]# echo ${array[*]}
ylt three four
[root@web001 scripts]# unset array
[root@web001 scripts]# echo ${array[*]}
5)数组内容的截取
[root@web001 scripts]# array=(1 2 3 4 5)
[root@web001 scripts]# echo ${array[@]:1:3}
2 3 4
[root@web001 scripts]# array=($(echo {a..z}))
[root@web001 scripts]# echo ${array[@]}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@web001 scripts]# echo ${array[@]:1:3}
b c d
[root@web001 scripts]# echo ${array[@]:0:2}
a b
替换数组元素部分内容:
[root@web001 scripts]# array=(1 2 3 1 1)
[root@web001 scripts]# echo ${array[@]/1/b}
b 2 3 b b
[root@web001 scripts]# echo ${array[@]}
1 2 3 1 1
删除数组元素部分内容:
[root@web001 scripts]# array=(one two three four five)
[root@web001 scripts]# echo ${array[@]}
one two three four five
[root@web001 scripts]# echo ${array[@]#o*} #<== 从左边开始匹配最短的数组元素,并删除
ne two three four five
[root@web001 scripts]# echo ${array[@]##o*} #<== 从左边开始匹配最长的数组元素,并删除
two three four five
[root@web001 scripts]# echo ${array[@]%f*} #<== 从右边开始匹配最短的数组元素,并删除
one two three
[root@web001 scripts]# echo ${array[@]%%f*} #<== 从右边开始匹配最长的数组元素,并删除
one two three
例 12-1
使用循环批量输出数组的元素
方法 1:通过 C 语言型的 for 循环语句打印数组元素。
#!/bin/bash
array=(1 2 3 4 5)
for ((i=0;i<${#array[@]};i++)
do
echo ${array[i]}
done
方法 2:通过普通 for 循环语句打印数组元素。
#!/bin/bash
array=(1 2 3 4 5)
for n in ${array[*]}
do
echo $n
done
方法 3:使用 while 循环语句打印元素。
array=(1 2 3 4 5)
i=0
while ((i<${#array[*]}))
do
echo ${array[i]}
((i++))
done
例 12-2
通过竖向列举法定义数组元素并批量打印、
#!/bin/bash
array=(
ylt
yyy
lll
ttt
)
for((i=0; i<${#array[@]}; i++))
do
echo "This is num $i, then content is ${array[$i]}"
done
例 12-3
将命令结果作为数组元素
[root@web001 scripts]# mkdir -p /array
[root@web001 scripts]# touch /array/array{1..3}
[root@web001 scripts]# ll /array/
total 0
-rw-r--r-- 1 root root 0 Nov 1 06:28 array1
-rw-r--r-- 1 root root 0 Nov 1 06:28 array2
-rw-r--r-- 1 root root 0 Nov 1 06:28 array3
[root@web001 scripts]# cat 266.sh
#!/bin/bash
dir=($(ls /array))
for ((i=0; i<${#dir[@]}; i++))
do
echo "This is No.$i, filename is ${dir[$i]}"
done
[root@web001 scripts]# sh 266.sh
This is No.0, filename is array1
This is No.1, filename is array2
This is No.2, filename is array3
1)定义命令
静态数组:
array=(1 2 3)
动态数组:
array=($(ls))
为数组赋值:
array[3]=4
2)打印命令
打印所有元素:
${array[@]} 或 ${array[*]}
打印数组长度:
${#array[@]} 或 ${array[*]}
打印单个元素:
${array[i]} #<== i 是数组下标
3)循环打印常用语法
#!/bin/bash
for ((i=0;i<${#array[@]}; i++))
do
echo "${array[$i]}"
done
#<== C 语言 for 循环语法
#!/bin/bash
for n in ${arary[@]}
do
echo "$n"
done
#<== 普通 for 循环语法
例 12-5
利用 bash for 循环打印下面这句话中字母数不大于 6 的单词。
I am learning the shell programming language
计算变量内容长度的方法:
char=I am learning the shell programming language
echo $char|wc -L
echo ${#char}
expr length $char
echo $char|awk '{print length($0)}'
方法 1:通过数组方法来实现
arr=(I am learning the shell programming language)
for ((i=0;i<${#arr[@]}; i++))
do
#if [ ${#arr[$i]} -le 6 ];then
echo "${arr[$i]}"
fi
done
echo ---------------------------------------------
for word in ${arr[@]}
do
#if [ `expr length $word` -le 6 ];then
if [ `echo $word|wc -L` -le 6 ];then
echo $word
fi
done
方法 2:通过 awk 循环实现
[root@web001 scripts]# chars="I am learning the shell programming language"
[root@web001 scripts]# echo $chars|awk '{for(i=1;i<=NF;i++) if(length($i)<=6) print $i}'
I
am
the
shell