shell第五次作业(函数和数组)

11 篇文章 0 订阅
11 篇文章 0 订阅

函数和数组

1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
3、编写函数实现两个数字做为参数,返回最大值
4、编写函数,实现两个整数位参数,计算加减乘除。

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

[root@wn2 test6]# vim 1.sh
[root@wn2 test6]# cat 1.sh 
#!/bin/bash
###################
#File name:1.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 10:51:52
#Description:
###################
fun (){
  if [ $# -ne 0 ]
  then
    echo -e "\033[32m OK \033[0m"
  else
    echo -e "\033[31m FAILED \033[0m"
  fi
}
read -p "请输入参数:" i
fun $i
[root@wn2 test6]# bash 1.sh 
请输入参数:12
 OK 
[root@wn2 test6]# bash 1.sh 
请输入参数:
 FAILED 

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

[root@wn2 test6]# vim 2.sh
[root@wn2 test6]# cat 2.sh 
#!/bin/bash
###################
#File name:2.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:05:41
#Description:
###################
fun() {
  if [ $# -eq 0 ]
  then
    echo "无位置参数"
  else
    echo "位置参数为$@"
  fi
}
read -p "请输入:" i
fun $i
[root@wn2 test6]# bash 2.sh 
请输入:21 43 54
位置参数为21 43 54
[root@wn2 test6]# bash 2.sh 
请输入:
无位置参数

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

[root@wn2 test6]# vim 3.sh
[root@wn2 test6]# cat 3.sh 
#!/bin/bash
###################
#File name:3.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:20:56
#Description:
###################
fun() {
  if [ $a -gt $b ]
  then
    echo "最大值为:$a"
  elif [ $a -eq $b ]
  then
    echo "a=b"
  else
    echo "最大值为:$b"
  fi
}
read -p "please input two numbers:" a b
fun $a $b
[root@wn2 test6]# bash 3.sh 
please input two numbers:12 4
最大值为:12
[root@wn2 test6]# bash 3.sh 
please input two numbers:4 5
最大值为:5
[root@wn2 test6]# bash 3.sh 
please input two numbers:4 4
a=b

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

[root@wn2 test6]# vim 4.sh
[root@wn2 test6]# cat 4.sh
#!/bin/bash
###################
#File name:4.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:26:35
#Description:
###################
fun() {
  echo a+b=$((a+b))
  echo a-b=$((a-b))
  echo a*b=$((a*b))
  echo a/b=$((a/b))
}
read -p "请输入两个整数位参数:" a b
fun $a $b
[root@wn2 test6]# bash 4.sh 
请输入两个整数位参数:12 4
a+b=16
a-b=8
a*b=48
a/b=3

如果加上参数个数及参数是否为整数,则shell脚本如下:

[root@wn2 test6]# vim 4.sh
[root@wn2 test6]# cat 4.sh 
#!/bin/bash
###################
#File name:4.sh
#Version:v1.0
#Email:admin@test.come
#Created time:2021-01-22 11:26:35
#Description:
###################
fun() {
  if [ $# -eq 2 ]
  then
    if [[ "$a" =~ ^[0-9]*$ && "$b" =~ ^[0-9]*$ ]]
    then
      echo a+b=$((a+b))
      echo a-b=$((a-b))
      echo a*b=$((a*b))
      echo a/b=$((a/b))
    else
      echo "两个参数不都为整数"
      exit 0
    fi
  else
    echo "参数不为两个"
    exit 0
  fi
}
read -p "请输入两个整数位参数:" a b
fun $a $b
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:14 2
a+b=16
a-b=12
a*b=28
a/b=7
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:i 2
两个参数不都为整数
[root@wn2 test6]# bash 4.sh
请输入两个整数位参数:12 3 4
参数不为两个
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值