示例1:执行shell脚本,打印一个如下的水果菜单:
1.apple
2.pear
3.banana
4.cherry
当用户输入对应的数字选择水果的时候,告诉他选择的水果是什么,并给水果单词加上一种颜色(随意),要求用case语句实现。
2.if嵌套:输入两个整数比较大小
解答:
示例一:
创建一个vim 04.sh
[root@LL test]# vim 04.sh
#!/bin/bash
echo "=========================================="
echo -e "\033[34;43;5m 请选择你喜欢的水果输入对应数字 \033[0m"
echo -e " \033[31m 1.apple \033[0m"
echo -e " \033[33m 2.peer \033[0m"
echo -e " \033[33m 3.banana \033[0m"
echo -e " \033[31m 4.cherry \033[0m"
echo "=========================================="
read -p " 选择你喜欢的水果编号(1-4): " num
case $num in
1)
echo -e " \033[31m 1.apple \033[0m"
;;
2)
echo -e " \033[33m 2.peer \033[0m"
;;
3)
echo -e " \033[33m 3.banana \033[0m"
;;
4)
echo -e " \033[31m 4.cherry \033[0m"
;;
*)
echo "你好菜,输入1-4之间的编号!!!"
esac
结果如下:
示例二:
#!/bin/bash
read -p "请输入第一个数值:" num1
read -p "请输入第二个数值: " num2
if [ $num1 -gt $num2 ] ;then
echo "Max=$num1","Min=$num2"
elif [ $num1 -eq $num2 ] ;then
echo 两个值相等
else
echo "Max=$num2","Min=$num1"
fi
#(1) let $numecho $? (2)echo $num | egrep `^[[:digit:]]+$` echo$? (3)[[ $num =~ ^[0-9]+$ ]]
if [[ $num =~ ^[0-9]+$ && $num2 =~ ^[0-9]+$ ]];then
if [ $num1 -gt $num2 ] ;then
echo "Max=$num1","Min=$num2"
elif [ $num1 -eq $num2 ] ;then
echo 两个值相等
else
echo "Max=$num2","Min=$num1"
fi
else
echo "好菜啊,输入的必须是数字哦!!!"
fi