数组
1.数组的定义方法
方法1:
[root@localhost mnt]# array1=(1 2 3)
##输出数组的所有元素
[root@localhost mnt]# echo ${array1[*]}
1 2 3
[root@localhost mnt]# echo ${array1[@]}
1 2 3
方法2:
[root@localhost mnt]# array2=([1]=one [2]=two [3]=three)
[root@localhost mnt]# echo ${array2[*]}
one two three
##输出数组的第一个元素
[root@localhost mnt]# echo ${array2[1]}
one
[root@localhost mnt]# echo ${array2[2]}
two
[root@localhost mnt]# echo ${array2[3]}
three
方法3:
##数组的下标一般是从0开始的
[root@localhost mnt]# array3[0]=a
[root@localhost mnt]# array3[1]=b
[root@localhost mnt]# array3[2]=c
[root@localhost mnt]# echo ${array3[*]}
a b c
方法4:
# 动态定义数组变量,并使用命令的输出结果作为数组的内容
[root@localhost mnt]# mkdir /array
[root@localhost mnt]# touch /array/file{1..3}
[root@localhost mnt]# ls /array/
file1 file2 file3
[root@localhost mnt]# array4=($(ls /array/))
[root@localhost mnt]# echo ${array4[*]}
file1 file2 file3
2.数组的打印
(1)打印数组元素
[root@localhost mnt]# array=(1 2 3)
[root@localhost mnt]# echo ${array[0]}
1
[root@localhost mnt]# echo ${array[1]}
2
[root@localhost mnt]# echo ${array[2]}
3
[root@localhost mnt]# echo ${array[*]}
1 2 3
[root@localhost mnt]# echo ${array[@]}
1 2 3
(2)打印数组个数
[root@localhost mnt]# echo ${#array[*]}
3
[root@localhost mnt]# echo ${#array[@]}
3
(3)数组的赋值
##如果下标不存在,则会自动添加一个新的元素,如果存在,则会覆盖原来的值
[root@localhost mnt]# echo ${array[@]}
1 2 3
[root@localhost mnt]# array[2]=three
[root@localhost mnt]# echo ${array[@]}
1 2 three
[root@localhost mnt]# array[0]=one
[root@localhost mnt]# echo ${array[@]}
one 2 three
[root@localhost mnt]# echo ${array[@]}
one 2 three four
(4)数组的删除
##删除数组的第一个元素
[root@localhost mnt]# unset array[0]
[root@localhost mnt]# echo ${array[@]}
2 three four
##删除数组中的所有元素
[root@localhost mnt]# unset array
[root@localhost mnt]# echo ${array[@]}
(5)数组内容的截取与替换
扫描二维码关注公众号,回复:
5352068 查看本文章
截取:
[root@localhost mnt]# array=(1 2 3 4 5)
[root@localhost mnt]# echo ${array[@]}
1 2 3 4 5
##截取下标为1和3的数组元素
[root@localhost mnt]# echo ${array[@]:1:3}
2 3 4
[root@localhost mnt]# array=($(echo {a..z}))
[root@localhost mnt]# 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@localhost mnt]# echo ${array[@]:0:3}
a b c
替换:
[root@localhost mnt]# array=(1 2 3 1 1)
[root@localhost mnt]# echo ${array[@]/1/a}
a 2 3 a a
练习1:
输出数组中元素的下标
分析:
##定义数组
[root@localhost mnt]# array=(1 2 3 4 5)
##输出数组的长度;即为数组元素的个数
[root@localhost mnt]# echo ${#array[*]}
5
编写脚本:
# 注意:for i in `seq 0 5` ##表示变量i的值从0(每次自加1)变到5
[root@localhost mnt]# vim array01.sh
#####################
#!/bin/bash
array=(1 2 3 4 5)
for i in `seq 0 ${#array[*]}`
do
echo "${array[i]}"
done
执行脚本:
[root@localhost mnt]# sh array01.sh
练习2:(面试题)
利用for循环打印下面这句话中字母个数小于6的单词
I am westos teacher welcome to westos training class
分析:
##定义数组
[root@localhost mnt]# arrary=(I am westos teacher welcome to westos training class)
##输出数组的所有元素
[root@localhost mnt]# echo ${arrary[*]}
I am westos teacher welcome to westos training class
##输出数组中下标为3的数组元素
[root@localhost mnt]# echo ${arrary[3]}
teacher
##输出数组的长度
[root@localhost mnt]# echo ${#arrary[*]}
9
##输出数组中下标为3的数组元素的长度
[root@localhost mnt]# echo ${#arrary[3]}
7
编写脚本:
[root@localhost mnt]# vim array02.sh
#####################
#!/bin/bash
arrary=(I am westos teacher welcome to westos training class)
for i in `seq 0 $[${#arrary[*]}-1]` ##必须是长度减1,否则输出会多一个空格
do
if [ ${#arrary[i]} -lt 6 ];then
echo "${arrary[i]}"
fi
done
执行脚本:
[root@localhost mnt]# sh array02.sh
随机数
##1.生成随机数(数值范围:0~32767)
[root@localhost mnt]# echo $RANDOM
19098
[root@localhost mnt]# echo $RANDOM
28298
[root@localhost mnt]# echo $RANDOM
9842
##2.生成随机字符(base64 40:64位的字符串40位)
[root@localhost mnt]# openssl rand -base64 40
Eq0CxtN4eb+gCpAxJYn78p3mpTXNP9h7KhQRtWkXA9a6K/eOIhlRXg==
[root@localhost mnt]# openssl rand -base64 40
eU2LuL4VNUCmHLfK5Z0/8DRC6TTRvtRwLW+2DaBxl2ad/Ga68rEpyQ==
[root@localhost mnt]# openssl rand -base64 40
p/6pAVUxSomcyQuugd+eLVfBdds8+6Z4CrJBlb5ceHwyWa8qS0nXJQ==
练习:
生成10个用户wesots01-westos10,并为其设定密码为10位的随机数
编写脚本:
[root@localhost mnt]# vim rand_useradd.sh
#####################
#!/bin/bash
. /etc/init.d/functions
user="westos"
passfile="/root/Desktop/userfile"
for num in `seq -w 10`
do
pass="`echo $RANDOM | md5sum | cut -c 7-14`"
useradd $user$num &> /dev/null && {
echo "$pass" | passwd --stdin $user$num &> /dev/null
echo -e "user:$user$num\tpasswd:$pass" >> $passfile
}
if [ $? -eq 0 ];then
action "$user$num is ok" /bin/true
else
action "$user$num is failed" /bin/false
fi
done
执行脚本:
[root@localhost mnt]# sh rand_useradd.sh
[root@localhost mnt]# cat /root/Desktop/userfile