shell第五天练习

本文提供了几个使用Bashshell脚本编写的函数示例,包括检查命令行参数并打印彩色反馈、验证无位置参数、找到两个数字中的最大值、执行基本算术运算以及对文件内容进行统计分析。示例涵盖了从文件读取到关联数组的使用,展示了在Linux环境中处理命令行输入和文件数据的基本技巧。
摘要由CSDN通过智能技术生成

题目:

1、编写函数,实现打印绿色OK和红色FAILED,判断是否有参数,存在为Ok,不存在为FAILED
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
3、编写函数实现两个数字做为参数,返回最大值
4、编写函数,实现两个整数位参数,计算加减乘除。
5、将/etc/shadow文件的每一行作为元素赋值给数组
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
7、使用关联数组按扩展名统计指定目录中文件的数量

1、编写函数,实现打印绿色OK和红色FAILED,判断是否有参数,存在为Ok,不存在为FAILED

1、编写脚本

[root@localhost day5]# vim echo_color.sh

#!/bin/bash
echo_color() {
  if [ $# -eq 0 ];then
	echo -e "\e[31mFAILED\e[0m"
  else
	echo -e "\e[32mOK\e[0m"
  fi
}
echo_color $@

2、设置权限

[root@localhost day5]# chmod a+rx echo_color.sh

3、运行测试
在这里插入图片描述

2、编写函数,实现判断是否无位置参数,如无参数,提示错误

1、编写脚本

[root@localhost day5]# vim if_no_param.sh

#!/bin/bash
if_no_param() {
  if [ $# -eq 0 ];then
	echo "error:There are no parameters"
  fi
}
if_no_param $@

2、设置权限

[root@localhost day5]# chmod a+rx if_no_param.sh

3、运行测试
在这里插入图片描述

3、编写函数实现两个数字做为参数,返回最大值

1、编写脚本

[root@localhost day5]# vim max_num.sh

#!/bin/bash
max_num() {
  if [ $# -ne 2 ];then
        echo "must have only 2 parameters"
  elif ! expr $1 + $2 &> /dev/null;then
        echo "the 2 parameters must be integer"
  else
        if [ $1 -gt $2 ];then
                echo $1
        else
                echo $2
        fi
  fi
}
max_num $@

2、设置权限

[root@localhost day5]# chmod a+rx max_num.sh

3、运行测试

[root@localhost day5]# ./max_num.sh 22 33
33
[root@localhost day5]# ./max_num.sh 15 6
15

4、编写函数,实现两个整数位参数,计算加减乘除。

1、编写脚本

[root@localhost day5]# vim calculation.sh

#!/bin/bash
calculation() {
  if [ $# -ne 2 ];then
	echo "must have only 2 parameters"
  elif ! expr $1 + $2 &> /dev/null;then
	echo "the 2 parameters must be integer"
  else
	echo "$1+$2=$[$1+$2]"
	echo "$1-$2=$[$1-$2]"
	echo "$1*$2=$[$1*$2]"
	echo "$1/$2=$[$1/$2]"
  fi
}
calculation $@

2、设置权限

[root@localhost day5]# chmod a+rx calculation.sh

3、运行测试

[root@localhost day5]# ./calculation.sh 3 4
3+4=7
3-4=-1
3*4=12
3/4=0
[root@localhost day5]# ./calculation.sh 15 3
15+3=18
15-3=12
15*3=45
15/3=5

5、将/etc/shadow文件的每一行作为元素赋值给数组

1、编写脚本

[root@localhost day5]# vim array_shadow.sh

#!/bin/bash
array=(`cat /etc/shadow`)
echo ${#array[@]}

2、设置权限

[root@localhost day5]# chmod a+rx array_shadow.sh

3、运行测试

[root@localhost day5]# cat /etc/shadow | wc -l
52
[root@localhost day5]# ./array_shadow.sh
52

6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量

1、编写脚本

[root@localhost day5]# vim shell_count.sh

#!/bin/bash
declare -A array
for line in `cut -d: -f7 /etc/passwd`
do
	let array[$line]+=1
done
for index in ${!array[*]}
do
	echo $index:${array[$index]}
done

2、设置权限

[root@localhost day5]# chmod a+rx shell_count.sh

3、运行测试

[root@localhost day5]# ./shell_count.sh 
/sbin/nologin:43
/bin/bash:6
/sbin/halt:1
/bin/sync:1
/sbin/shutdown:1

7、使用关联数组按扩展名统计指定目录中文件的数量

1、编写脚本

[root@localhost day5]# vim file_count.sh

#!/bin/bash
declare -A array
read -p "please input a directory path:" dir
if ! [ -d "$dir" ];then
	echo "your input is not a directory"
else
	for i in `ls $dir`
	do
		let array[${i#*.}]+=1
	done
fi
for index in ${!array[*]}
do
	echo "$index:${array[$index]}"
done

2、设置权限

[root@localhost day5]# chmod a+rx file_count.sh

3、运行测试
在这里插入图片描述

[root@localhost day5]# ./file_count.sh
please input a directory path:/root/shell/day5
jpg:2
txt:3
zip:2
sh:7
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值