shell 函数和数组作业

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

1、编写函数,实现打印绿色OK和红色FAILED,判断是否有参数,存在为Ok,不存在为FAILED
[root@localhost test]# vim 1.sh
#!/bin/bash
face(
{
    if [ -z "$1" ];then
        echo -e "\e[1;32m OK \e[0m"
    else
        echo -e "\e[1;31m FLASE \e[0m"
    fi
}
face $1

在这里插入图片描述

2、编写函数,实现判断是否无位置参数,如无参数,提示错误
[root@localhost test]# vim 2.sh
#!/bin/bash
face()
{
    if [ "$1" -gt 0 ];then
        echo "有位置参数"
    else
        echo "无位置参数"
    fi
}
face $#
[root@localhost test]# bash 2.sh 1
有位置参数
[root@localhost test]# bash 2.sh 
无位置参数
3、编写函数实现两个数字做为参数,返回最大值
[root@localhost test]# vim 3.sh
#!/bin/bash
read -p "输入两个整数:" a b
[ -z $a -o -z $b ] && read -p "输入不能为空,重新输入:" a b
expr $a + 1 &>/dev/null
err1=$?
expr $b + 1 &>/dev/null
err2=$?
[ "$err1" -ne 0 -o "$err2" -ne 0 ] && read -p "输入错误,重新输入:" a b
face()
{
    if [ "$1" -gt "$2" ];then
        echo "$1"
    else
        echo "$2"
    fi
}
face $a $b
[root@localhost test]# bash 3.sh 
输入两个整数:34 34
34
[root@localhost test]# bash 3.sh 
输入两个整数:34 24
34
4、编写函数,实现两个整数位参数,计算加减乘除
[root@localhost test]# vim 4.sh
#!/bin/bash
read -p "输入两个整数:" a b
[ -z $a -o -z $b ] && read -p "输入不能为空,重新输入:" a b
expr $a + 1 &>/dev/null
err1=$?
expr $b + 1 &>/dev/null
err2=$?
[ "$err1" -ne 0 -o "$err2" -ne 0 ] && read -p "输入错误,重新输入:" a b
face()
{
    echo "$1+$2="$[$1+$2]
    echo "$1-$2="$[$1-$2]
    echo "$1*$2="$[$1*$2]
    echo "$1/$2="$[$1/$2]
}
face $a $b
[root@localhost test]# bash 4.sh 
输入两个整数:
输入不能为空,重新输入:a d
输入错误,重新输入:8 4
8+4=12
8-4=4
8*4=32
8/4=2
5、将/etc/shadow文件的每一行作为元数赋值给数组
[root@localhost test]# vim 5.sh
#!/bin/bash
i=0
while read line
do
    array[$i]=$line
    let i++
done < /etc/shadow

for i in ${array[*]}
do
  echo $i
done
6、使用关联数组统计文件/etc/passwd中用户使用的不同类型shell的数量
[root@localhost test]# vim 6.sh
#!/bin/bash
shell=`cut -d : /etc/passwd -f7 | sort -n | uniq`
file=`cut -d : /etc/passwd -f7 | sort -n`
declare -A array
for i in $shell
do
    k=0
    for j in $file
    do
        if [ "$i" = "$j" ];then
            let k++
        fi
        array[$i]=$k
    done
done
for i in $shell
do
    echo "array[$i]="${array[$i]}
done
[root@localhost test]# bash 6.sh
array[/bin/bash]=23
array[/bin/false]=1
array[/bin/sync]=1
array[/sbin/halt]=1
array[/sbin/nologin]=33
array[/sbin/shutdown]=1
array[/usr/sbin/nologin]=2
7、使用关联数组按扩展名统计指定目录中文件的数量
[root@localhost test]# vim 7.sh
#!/bin/bash
read -p "输入目录文件:" path
[ -z $path ] && read -p "输入错误,重新输入:" path
shell=`ls $path | cut -d . -f2 | sort -n | uniq`
declare -A array
for i in $shell
do
    k=0
    for j in `ls $path | cut -d . -f2`
    do
        if [ "$i" = "$j" ];then
            let k++
        fi
        array[$i]=$k
    done
done
for i in $shell
do
    echo "array[$i]="${array[$i]}
done
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值