shell第三次作业

函数和数组
1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED
2、编写函数,实现判断是否无位置参数,如无参数,提示错误
3、编写函数实现两个数字做为参数,返回最大值
4、编写函数,实现两个整数
1、编写函数,实现打印绿色OK和红色FAILED 判断是否有参数,存在为Ok,不存在为FAILED位参数,计算加减乘除。
vim 1.sh
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.
vim 2.sh
#!/bin/bash
fun() {
if [ KaTeX parse error: Expected 'EOF', got '#' at position 1: #̲ -eq 0 ] then…@"
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.
vim 3.sh
fun() {
if [ $a -gt b ] t h e n e c h o " 最 大 值 为 : b ] then echo "最大值为: b]thenecho"a"
elif [ $a -eq b ] t h e n e c h o " a = b " e l s e e c h o " 最 大 值 为 : b ] then echo "a=b" else echo "最大值为: b]thenecho"a=b"elseecho"b"
fi
}
read -p “please input two numbers:” a b
fun $a KaTeX parse error: Expected 'EOF', got '#' at position 19: …root@wn2 test6]#̲ bash 3.sh ple…((a+b))
echo a-b= ( ( a − b ) ) e c h o a ∗ b = ((a-b)) echo a*b= ((ab))echoab=((ab))
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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值