shell函数练习题

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

#!/bin/bash
#########################
#File name:01.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-11 23:03:28
#Description:
#########################
printf(){
if [ $1 ]
then
        echo -e "\033[32m ok \033[0m"
else
        echo -e "\033[31m filed \033[0m"
fi
}

printf $1

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

#!/bin/bash
#########################
#File name:02.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-11 23:37:44
#Description:
#########################
EoN(){
if [ $1 ]
then
        echo "参数为$num"
else
        echo "错误,无位置参数"
fi
}
read -p "请输入参数:" num
EoN $num

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

#!/bin/bash
#########################
#File name:03.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-11 23:42:29
#Description:
#########################
max(){
if [ $num1 -gt $num2 ]
then
        echo "max为$num1"
else
        echo "max为$num2"
fi



}

read -p "请输入两个数:"  num1 num2
max $num1,$num2

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

#!/bin/bash
#########################
#File name:04.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-11 23:49:22
#Description:
#########################

count(){
result=`expr $num1 + $num2`
echo $num1"+"$num2"=" "$result"
result=`expr $num1 - $num2`
echo $num1"-"$num2"=" "$result"


result=`expr $num1 \* $num2`
echo $num1"*"$num2"=" "$result"

result=`expr $num1 / $num2`
echo $num1"/"$num2"=" "$result"
}

read -p "请输入两个数:"  num1 num2
count $num1,$num2

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

#!/bin/bash
#########################
#File name:05.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-12 00:02:38
#Description:
#########################
file=/etc/shadow
num=`wc -l < $file `
for i in `seq $num`
do
        arroy[$i]=`head -$i $file | tail -l`
done

for i in ${arryoy[*]}
do
        echo $i
done

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

#!/bin/bash
#########################
#File name:06.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-12 00:09:38
#Description:
#########################
#! /bin/bash
declare -A shells
while read line
do
   type=`echo $line | awk -F: '{print $7}'`
   let shells[$type]++
done < /etc/passwd

for i in ${!shells[*]}
do
   echo "$i 的数量为 ${shells[$i]}"
done

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

#!/bin/bash
#########################
#File name:07.sh
#Version:v1.0
#Email:wjs.com
#Created time:2023-04-12 00:10:33
#Description:
#########################
#! /bin/bash
read -p "请输入要查看的目录:" path
[ -z $path ]&& echo "输入错误:输入为空" && exit 1
cd $path
for i in `ls`
do
        if [ -d $i ]; then
          echo $i >>/hanshuFile/ceshi.txt
        fi
done
declare -A arroy
a=0
while read line
do
  let arroy[$a]++
done </hanshuFile/ceshi.txt
b=0
for b in ${!arroy[*]}
do
   echo "目录文件共有: ${arroy[$b]}"
done
rm -rf /hanshuFile/ceshi.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值