shell5

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

编写脚本文件

#!/bin/bash
#########################
#File name:1.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 17:00:21
#Description:
#########################
color() {
if [ $# -eq 0 ];then
        echo -e "\e[31mFAILED\e[0m"
  else
        echo -e "\e[32mOK\e[0m"
  fi
}
color $*

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 1.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:2.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 17:14:22
#Description:
#########################
parameter() {
if [ $# -eq 0 ];then
        echo "error!"
fi
}
parameter $*

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 2.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:3.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 17:31:08
#Description:
#########################
max_number() {
if [ $# -ne 2 ];then
        echo "must have two numbers!"
elif ! expr $1 + $2  &> /dev/null;then
        echo "must have number!"
else
        if [ $1 -gt $2 ];then
                echo $1
        else
                echo $2
        fi
fi
}
max_number $*

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 3.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:4.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 17:47:04
#Description:
#########################
operation() {
if [ $# -ne 2 ];then
        echo "must have two numbers!"
elif ! expr $1 + $2  &> /dev/null;then
        echo "must have number!"
else
        echo "$1+$2=$[$1+$2]"
        echo "$1-$2=$[$1-$2]"
        echo "$1*$2=$[$1*$2]"
        echo "$1/$2=$[$1/$2]"
fi
}
operation $*

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 4.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:5.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 17:55:39
#Description:
#########################
n=`cat /etc/shadow`
for ((i=0;i<=$n;i++))
do
        array[$i]=$(head -$i /etc/shadow | tail -l)
done
for j in ${array[*]}
do
        echo $j
done

给文件添加r,x权限

[root@zyf shell6]# chmod a+rx 5.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:6.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 18:09:30
#Description:
#########################
declare -A shells
for i in `cut -d : -f7 /etc/passwd`
do
        let shells[$i]+=1
done < /etc/passwd
for j in ${!shells[*]}
do
        echo "$j : ${shells[$j]}"
done

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 6.sh

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

编写脚本文件

#!/bin/bash
#########################
#File name:7.sh
#Version:v1.0
#Email:admin@test.com
#Created time:2023-01-02 18:25:26
#Description:
#########################
declare -A array
read -p "please input a dir path:" dir
if ! [ -d "$dir" ];then
        echo "must input a dir!"
else
        for i in `ls $dir`
        do
                let array[${i#*.}]+=1
        done
fi
for j in ${!array[*]}
do
        echo "$j: ${array[$j]}"
done

给文件添加r,x权限,并查看运行结果

[root@zyf shell6]# chmod a+rx 7.sh

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值