函数和数组
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=
((a−b))echoa∗b=((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
ab=48
a/b=3
shell第三次作业
最新推荐文章于 2023-04-02 15:45:16 发布