函数和数组

1 、编写函数,实现打印绿色 OK 和红色 FAILED
判断是否有参数,存在为 Ok ,不存在为 FAILED
[root@localhost test]#  echo -e "\033[31m FAILED \033[0m"
 FAILED 
[root@localhost test]#  echo -e "\033[32m ok \033[0m"
 ok 
#!/bin/bash
read -p "please input arg:" arg
color() {
if [ $# -ne 0 ];then
        echo -e "\033[31m OK \033[0m"
else
        echo -e "\033[32m FAILED \033[0m"
fi
}

color $arg
[root@localhost test]# chmod a+x color.sh
[root@localhost test]# ./color.sh
please input arg:
 FAILED 
[root@localhost test]# ./color.sh
please input arg:1
 OK 
2 、编写函数,实现判断是否无位置参数,如无参数,提示错误
#!/bin/bash

read -p "please input arg:" arg
func() {
if [ $# -ne 0 ];then
  echo $arg
else
  echo "错误"
fi
}

func $arg 
[root@localhost test]# chmod a+x 6.2.sh

[root@localhost test]# ./6.2.sh
please input arg:1
1
[root@localhost test]# ./6.2.sh
please input arg:
错误
3 、编写函数实现两个数字做为参数,返回最大值
[root@localhost test]# vim 6.3.sh
#!/bin/bash

read -p "please input two num:" a b
func() {
if [ $a -gt $b ];then
  echo "最大值为$a"
elif [ $b -gt $a ];then
  echo "最大值为$b"
else
  echo "相等"
fi
}

func $a $b

[root@localhost test]# chmod a+x 6.3.sh
[root@localhost test]# ./6.3.sh
please input two num:5 8
最大值为8
[root@localhost test]# ./6.3.sh
please input two num:9 3
最大值为9
[root@localhost test]# ./6.3.sh
please input two num:6 6
相等
4 、编写函数,实现两个整数位参数,计算加减乘除。
[root@localhost test]# vim 6.4.sh
#!/bin/bash
read -p "please input two num:" a b
func () {
[ $# -ne 2 ] && {
  echo "usage: $0 num1 num2"
  exit 1
}
expr $a + $b  &> /dev/null
if [ $? -ne 0 ]
then
  echo "you must input two number" 
  exit 2
fi
echo "$a+$b=$(($a+$b))" 
echo "$a-$b=$(($a-$b))" 
echo "$a*$b=$(($a*$b))" 
echo "$a/$b=$(($a/$b))"
}

func $a $b

[root@localhost test]# chmod a+x 6.4.sh
[root@localhost test]# ./6.4.sh
please input two num:3 4
3+4=7
3-4=-1
3*4=12
3/4=0
[root@localhost test]# ./6.4.sh
please input two num:w r
you must input two number
5 、将 /etc/shadow 文件的每一行作为元数赋值给数组
[root@localhost test]# vim 6.5.sh
#!/bin/bash
declare -a array
i=0
while read line
do
  array[$i]=$line
  echo ${array[$i]}
  let i++
done < /etc/shadow

[root@localhost test]# chmod a+x  6.5.sh
6 、使用关联数组统计文件 /etc/passwd 中用户使用的不同类型 shell 的数量
[root@localhost test]# vim 6.6_7.sh
#!/bin/bash
declare -A array
for i in `cut -d: -f 7 /etc/passwd`
do
  let array[$i]++
done
for i in ${!array[*]}
do
  echo "$i ${array[$i]}"
done
[root@localhost test]# chmod a+x 6.6_7.sh
[root@localhost test]# ./6.6_7.sh
/sbin/nologin 35
/bin/bash 23
/sbin/halt 1
/bin/sync 1
/sbin/shutdown 1
7 、使用关联数组按扩展名统计指定目录中文件的数量
[root@localhost test]# vim 6.7_1.sh
#!/bin/bash
read -p "please iuput dir:" d
declare -A array
for i in `ls -F $d | grep -v /$ | fgrep . | cut -d . -f 2`
do
  let array[$i]++
done
for i in ${!array[*]}
do
  echo "$i ${array[$i]}"
done
[root@localhost test]# chmod a+x 6.7_1.sh
[root@localhost test]# ./6.7_1.sh
please iuput dir:/root/
cfg 1
sh 1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值