数组:
数组的定义:在集合当中指定多个元素,元素的类型:整数,字符串,浮点数。
数组的作用:可以一次性的定义多个元素,可以为变量赋值提供便利。
数组的定义方法:
数组名=(a b c d)
数组名不能重复

#数组的定义方法
[root@localhost opt]# test1=(a b c d)
[root@localhost opt]# echo ${test1[*]}
a b c d
[root@localhost opt]# echo ${test1[@]}
a b c d
[root@localhost opt]# test2[0]=1
[root@localhost opt]# test2[1]=2
[root@localhost opt]# test2[2]=3
[root@localhost opt]# echo ${test2[*]}
1 2 3
数组的长度指的是数组内包含了几个元素。
[root@localhost opt]# echo ${#test1[*]}
4
数组的便利方式
#数组的便利方式
test4=(1 2 3 4 5)
for num in ${test4[*]}
do
echo $num
done
数组的切片
[root@localhost opt]# test5=(1 2 3 4 5 6)
[root@localhost opt]# echo ${test5[*]:1:3}
2 3 4
[root@localhost opt]# echo ${test5[*]:0:2} #0表示起始位置,2表示步长,起始位置0开始,包括0,移2个。
1 2
数组的替换
临时替换:
[root@localhost opt]# echo ${test5[*]/4/99}
1 2 3 99 5 6
[root@localhost opt]# echo ${test5[*]}
1 2 3 4 5 6
永久替换:通过修改元素下标的值可以实现。
[root@localhost opt]# test5[3]=99
[root@localhost opt]# echo ${test5[*]}
1 2 3 99 5 6
删除数组
[root@localhost opt]# echo ${test1[*]}
a b c d
[root@localhost opt]# unset test1
[root@localhost opt]# echo ${test1[*]}
[root@localhost opt]# echo ${test5[*]}
1 2 3 99 5 6
[root@localhost opt]# unset test5[3]
[root@localhost opt]# echo ${test5[*]}
1 2 3 5 6
数组追加,追加元素
指定下标位置进行追加
[root@localhost opt]# test5[3]=4
自动追加
[root@localhost opt]# test5+=(7 8)
[root@localhost opt]# echo ${test5[*]}
1 2 3 4 5 6 7 8
练习
#现在定义一个数组,元素都是整数,实现数组内整数的累加求和。
num=0
sum=0
test1=(10 21 30 41 50 61 70 81 90)
for i in ${test1[*]}
do
if (( $i%2 == 0 ))
thennum=$(($i+$num))
elsesum=$(($i+$sum))
fi
done
echo "偶数和是:" $num
echo "奇数和是:" $sum
#定义一个数组,使用条件判断找出数组内最大值和最小值
test1=(3 5 7 4 9)
a=${test1[0]}
b=${test1[0]}
for i in ${test1[*]}
do
if [[ $i -gt $b ]]
thenb=$i
fi
if [[ $i -lt $a ]]thena=$ifi
done
echo $b
echo $a
冒号排序:
test1=(20 10 60 40 50 30)
#从小到大排序
#思路:对比两个相邻的元素,从小到大为例。满足交换条件的元素,小的往左移,大的往右移。
#数组的位置发生变化(下标对应的元素的值发生变化)
#双层循环,外部循环控制排序的轮次。内循环比较两个元素的大小,决定是否互换位置。
#对比和交换的次数随着排序轮次而减少。
echo "原数组的排序为:${test1[*]}"
length=${#test1[*]}
for ((i=1;i<$length;i++))
dofor ((j=0;j<$length-i;j++))doa=${test1[$j]}c=$(($j+1))b=${test1[$c]}if [[ $a -gt $b ]]thend=$atest1[$j]=$btest1[$c]=$d
fidone
done
echo "${test1[*]}"
[root@localhost opt]# sh shuzu3.sh
原数组的排序为:20 10 60 40 50 30
10 20 30 40 50 60
正则表达式:
正则表达式匹配的是文本内容,linux的文本三剑客 都是针对文本内容
文本三剑客:
grep 过滤文本内容
sed 针对文本内容进行增删改查
awk 按行取列
文本三剑客都是按行进行匹配。
grep的作用就是使用正则表达式来匹配文本内容。
选项:
-m 匹配几次之后停止
[root@localhost opt]# grep -m 1 root /etc/passwd root:x:0:0:root:/root:/bin/bash
-v 取反
-n 显示匹配的行号
[root@localhost opt]# grep -n root /etc/passwd 1:root:x:0:0:root:/root:/bin/bash 10:operator:x:11:0:operator:/root:/sbin/nologin
-c 只统计匹配的行数
[root@localhost opt]# grep -c root /etc/passwd 2
-o 仅显示匹配的结果
[root@localhost opt]# grep -o root /etc/passwd root root root root
-q 静默模式。不输出任何信息
[root@localhost opt]# grep -q root /etc/passwd [root@localhost opt]#
-A 数字 后几行
[root@localhost opt]# grep -A 3 root /etc/passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin -- operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin nobody:x:99:99:Nobody:/:/sbin/nologin
-B 数字 前几行
-C 数字 前后各几行
-e 或者
-E 匹配扩展正则表达式
-f 匹配两个文件相同的内容,以第一个文件为准
[root@localhost opt]# vim 123.txt qwe 123 aaa bbb ccc [root@localhost opt]# vim 456.txt 123 qwe ddd ccc [root@localhost opt]# grep -f 123.txt 456.txt 123 qwe ccc
-r 递归目录 目录下的文件内容。软连接不包含在内
-R 递归目录 目录下的文件内容。包含软连接。
[root@localhost opt]# grep -r qwe /opt /opt/dec/123.txt:qwe /opt/123.txt:qwe /opt/456.txt:qwe [root@localhost opt]# grep -R qwe /opt /opt/dec/123.txt:qwe /opt/123.txt:qwe /opt/999.txt:qwe /opt/456.txt:qwe
排序:
sort
以行为单位,对文件的内容进行排序
sort 选项 参数
cat file | sort 选项
-f 忽略大小写,默认会把大写字母排在前面
-b 忽略每行之前的空格
-n 按照数字进行排序
-r 反向排序
-u 相同的数据仅显示一行
-o 把排序后的结构转存到指定的文件
uniq 去除连续重复的行,只显示一行
-c 统计连续重复的行的次数,合并连续重复的行
-u 显示仅出现一次的行(包括不是连续出现的重复行)
-d 仅显示连续重复的行(不包括非连续出现的内容)
作业:
按照大小进行排序
for k in `df -h | awk 'NR>1 {print $5}' | tr -d '%'`
dotest2+=($k)
length=${#test2[*]}for ((i=1;i<$length;i++))dofor ((j=0;j<$length-i;j++))doa=${test2[$j]}c=$(($j+1))b=${test2[$c]}if [[ $a -lt $b ]]thend=$atest2[$j]=$btest2[$c]=$d
fidone
done
done
echo ${test2[*]}
[root@localhost opt]# sh shuzu5.sh
100 39 18 15 1 1 1 1 0 0 0 0
test2=($(df -h | awk 'NR>1 {print $5}' | tr -d '%'))
length=${#test2[*]}for ((i=1;i<$length;i++))dofor ((j=0;j<$length-i;j++))doa=${test2[$j]}c=$(($j+1))b=${test2[$c]}if [[ $a -lt $b ]]thend=$atest2[$j]=$btest2[$c]=$d
fidone
done
echo ${test2[*]}
df -h | awk 'NR>1 {print $0}' | sort -k5 -nr
[root@localhost opt]# sh shuzu5.sh
100 39 18 15 1 1 1 1 0 0 0 0
/dev/sr0 4.3G 4.3G 0 100% /mnt
/dev/sdb2 17M 6.5M 11M 39% /data2
/dev/sda1 1014M 179M 836M 18% /boot
/dev/mapper/centos-root 38G 5.5G 32G 15% /
tmpfs 378M 12K 378M 1% /run/user/42
tmpfs 1.9G 13M 1.9G 1% /run
/dev/sdb3 5.0G 33M 5.0G 1% /data3
/dev/mapper/centos-home 19G 37M 19G 1% /home
tmpfs 378M 0 378M 0% /run/user/0
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
tmpfs 1.9G 0 1.9G 0% /dev/shm
devtmpfs 1.9G 0 1.9G 0% /dev
