目录
1.编写函数,实现打印绿色OK和红色FAILED,判断是否有参数,存在为OK,不存在为FAILED
2.编写函数,实现判断是否无位置参数,如无参数,提示错误
3.编写函数实现两个数字作为参数,返回最大值
4.编写函数,实现两个整数位参数,计算加减乘除
5.将/etc/shadow文件的每一行作为元素赋值给数组
6.使用关联数组统计文件/etc/passwd中用户使用不同类型shell的数量
7.使用关联数组按扩展名统计指定目录中文件的数量
代码部分
#!/bin/bash
#########################
#File name:test1.sh
#Author: BenJM
#Created time:2023-04-08 08:17:33
#########################
Print_Ok() {
echo -e "\033[32m OK \033[0m" #打印绿色OK函数
}
Print_Failed() {
echo -e "\033[31m FAILED \033[0m" #打印红色FAILED函数
}
if [ $# -eq 0 ]
then
Print_Failed #判断没有参数,则调用打印FAILED函数
else
Print_Ok #否则调用打印OK函数
fi
测试部分
[root@localhost shell_test2]# bash test1.sh
FAILED
[root@localhost shell_test2]# bash test1.sh haha
OK
代码部分
#!/bin/bash
#########################
#File name:test2.sh
#Author: BenJM
#Created time:2023-04-08 08:50:42
#########################
#判断是否有位置参数,没有则提示错误,否则输出所有参数
PD() {
if [ $# -eq 0 ]
then
echo "error"
else
echo "$*"
fi
}
#必须记得要调用函数
PD $*
测试部分
[root@localhost shell_test2]# bash test2.sh
error
[root@localhost shell_test2]# bash test2.sh hehe
hehe
代码部分
#!/bin/bash
#########################
#File name:test3.sh
#Author: BenJM
#Created time:2023-04-08 08:50:51
#########################
#函数,比较并输出最大值
max() {
if [ $num1 -gt $num2 ]
then
echo "$num1"
else
echo "$num2"
fi
}
#获取数据
read -p "please you input two integer:" num1 num2
#判断参数是否都为整数
expr $num1 + $num2 + 10 &> /dev/null
if [ $? -eq 0 ]
then
max $num1 $num2 #满足条件,调用max函数
else
echo "error,the parameter must be two integer" #否则提示错误
fi
测试部分
[root@localhost shell_test2]# bash test3.sh
please you input two integer: ab 98
error,the parameter must be two integer
[root@localhost shell_test2]# bash test3.sh
please you input two integer: 98 78
98
代码部分
#!/bin/bash
#########################
#File name:test4.sh
#Author: BenJM
#Created time:2023-04-08 08:50:57
#########################
#判断两数是否都为整数
PD() {
expr $num1 + $num2 + 10 &> /dev/null
if [ $? -eq 0 ]
then
return 0
else
echo "error"
return 1
fi
}
#计算加减乘除
calculate() {
echo $[$num1+$num2]
echo $[$num1-$num2]
echo $[$num1*$num2]
echo $[$num1/$num2]
}
#获取数据
read -p "please you input two integer: " num1 num2
if PD $num1 $num2
then
calculate $num1 $num2
else
echo "you must input two integer"
fi
测试部分
[root@localhost shell_test2]# bash test4.sh
please you input two integer: 4 haha
error
you must input two integer
[root@localhost shell_test2]# bash test4.sh
please you input two integer: 20 4
24
16
80
5
代码部分
#!/bin/bash
#########################
#File name:test5.sh
#Author: BenJM
#Created time:2023-04-08 08:51:03
#########################
declare -a array
i=0
echo "数组对应下标及元素如下:"
while read line
do
array[$i]=$line
echo "$i--> ${array[$i]}"
let i++
done < /etc/shadow
测试部分
[root@localhost shell_test2]# bash test5.sh
数组对应下标及元素如下:
0--> root:$6$3/DO0XIjKDSb0h7c$EW7CEA3f69d9LB/r7L6xiGylBzQejTaGCUmWOCmc005K2vkefpqJtmkeQTFElVh4JEVwYWpN3TXQafFmEnEew/::0:99999:7:::
1--> bin:*:18199:0:99999:7:::
2--> daemon:*:18199:0:99999:7:::
3--> adm:*:18199:0:99999:7:::
4--> lp:*:18199:0:99999:7:::
5--> sync:*:18199:0:99999:7:::
6--> shutdown:*:18199:0:99999:7:::
7--> halt:*:18199:0:99999:7:::
8--> mail:*:18199:0:99999:7:::
9--> operator:*:18199:0:99999:7:::
10--> games:*:18199:0:99999:7:::
11--> ftp:*:18199:0:99999:7:::
12--> nobody:*:18199:0:99999:7:::
13--> dbus:!!:19095::::::
14--> systemd-coredump:!!:19095::::::
15--> systemd-resolve:!!:19095::::::
16--> tss:!!:19095::::::
17--> polkitd:!!:19095::::::
18--> geoclue:!!:19095::::::
19--> rtkit:!!:19095::::::
20--> pulse:!!:19095::::::
21--> libstoragemgmt:!!:19095::::::
22--> qemu:!!:19095::::::
23--> usbmuxd:!!:19095::::::
24--> unbound:!!:19095::::::
25--> rpc:!!:19095:0:99999:7:::
26--> gluster:!!:19095::::::
27--> chrony:!!:19095::::::
28--> setroubleshoot:!!:19095::::::
29--> pipewire:!!:19095::::::
30--> saslauth:!!:19095::::::
31--> dnsmasq:!!:19095::::::
32--> radvd:!!:19095::::::
33--> clevis:!!:19095::::::
34--> cockpit-ws:!!:19095::::::
35--> cockpit-wsinstance:!!:19095::::::
36--> sssd:!!:19095::::::
37--> flatpak:!!:19095::::::
38--> colord:!!:19095::::::
39--> gdm:!!:19095::::::
40--> rpcuser:!!:19095::::::
41--> gnome-initial-setup:!!:19095::::::
42--> sshd:!!:19095::::::
43--> avahi:!!:19095::::::
44--> rngd:!!:19095::::::
45--> tcpdump:!!:19095::::::
46--> uuu:$6$RYJURo0i53P6EOjz$qQgwPHASpVT7AwWgw8eMRkY13Zf1kaYoUbAQQZvVBXpib/Xwf9rPYRsukbj61mLUI3Qhs8Iwdulf243R95.7I/:19434:0:99999:7:::
47--> xixi:!!:19307:0:99999:7:::
48--> apache:!!:19307::::::
49--> mysql:!!:19449::::::
代码部分
#!/bin/bash
#########################
#File name:test6.sh
#Author: BenJM
#Created time:2023-04-08 08:51:13
#########################
#用-A指定数组为关联数组
declare -A array
while read line
do
type=`echo $line | awk -F: '{print $7}'` #使用awk获取每一行的shell类型
let array[$type]++ #对应关联数组的值加一
done < /etc/passwd
#输出不同shell类型以及对应数量
for i in ${!array[@]}
do
echo "$i 对应数量为:${array[$i]}"
done
测试部分
[root@localhost shell_test2]# bash test6.sh
/sbin/nologin 对应数量为:44
/bin/bash 对应数量为:3
/sbin/halt 对应数量为:1
/bin/sync 对应数量为:1
/sbin/shutdown 对应数量为:1
代码部分
#!/bin/bash
#########################
#File name:test7.sh
#Author: BenJM
#Created time:2023-04-08 08:51:21
#########################
#用-A指定数组为关联数组
declare -A array
#使用awk获得每一个文件的扩展名并赋值给type
type=`ls | awk -F. '{print $NF}'`
#遍历并统计数量
for i in $type
do
let array[$i]++ #对应关联数组的值加一
done
#输出
for i in ${!array[@]}
do
echo "扩展名为$i,对应数量为:${array[$i]} "
done
测试部分
[root@localhost shell_test2]# ll
total 28
-rw-r--r--. 1 root root 429 Apr 8 08:46 test1.sh
-rw-r--r--. 1 root root 341 Apr 8 09:11 test2.sh
-rw-r--r--. 1 root root 559 Apr 8 10:06 test3.sh
-rw-r--r--. 1 root root 609 Apr 8 10:22 test4.sh
-rw-r--r--. 1 root root 299 Apr 8 10:31 test5.sh
-rw-r--r--. 1 root root 482 Apr 8 11:00 test6.sh
-rw-r--r--. 1 root root 478 Apr 8 11:20 test7.sh
[root@localhost shell_test2]# bash test7.sh
扩展名为sh,对应数量为:7
[root@localhost shell_test2]# touch xxx{1..3}.txt
[root@localhost shell_test2]# ll
total 28
-rw-r--r--. 1 root root 429 Apr 8 08:46 test1.sh
-rw-r--r--. 1 root root 341 Apr 8 09:11 test2.sh
-rw-r--r--. 1 root root 559 Apr 8 10:06 test3.sh
-rw-r--r--. 1 root root 609 Apr 8 10:22 test4.sh
-rw-r--r--. 1 root root 299 Apr 8 10:31 test5.sh
-rw-r--r--. 1 root root 482 Apr 8 11:00 test6.sh
-rw-r--r--. 1 root root 478 Apr 8 11:20 test7.sh
-rw-r--r--. 1 root root 0 Apr 8 11:21 xxx1.txt
-rw-r--r--. 1 root root 0 Apr 8 11:21 xxx2.txt
-rw-r--r--. 1 root root 0 Apr 8 11:21 xxx3.txt
[root@localhost shell_test2]# bash test7.sh
扩展名为txt,对应数量为:3
扩展名为sh,对应数量为:7