if then
1.语法
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi
2.使用案例
判断用户输入的数字是否大于100
if01.sh
#!/bin/bash/
#if then用法
#xie
#2018年5月19日10:24:28
read -p "input a number:" num
#判断用户输入的是否为数字
if [ "$num" -gt 0 ] 2>/dev/null ;then
if [ $num -le 100 ];then
echo "then number is letter than or equal 100 ";
else
echo "the number is gratter than 100 "
fi
#不为数字,结束
else
echo "input error"
fi
case
1.语法
case $变量名称 in
"第一个变量内容")
程序段
;;
"第二个变量内容")
程序段
;;
*)
不包含第一个变量内容与第二个变量内容的其它程序执行段
exit 1
;;
esac
语法是以 case 为开头,以 esac(case倒着写) 为结尾。每一个变量内容的程序段最后都需要两个分号 (;;) 来代表该程序段落的结束
2.使用案例
让用户输入one或者two或者three,然后判断它输入的是哪个
case01.sh
#!/bin/bash/
#case的使用,根据用户输入,判断它输入的是one还是two或者three
#xie
#2018年5月19日15:02:10
read -p "please input your choice:" choice
case $choice in
"one")
echo "your choice is one"
;;
"two")
echo "your choice is two"
;;
"three")
echo "your choice is three"
;;
*)
echo "Usage {one|two|three}"
;;
esac
function
1.用法
2.使用案例
将上面案例中输出的公共字符串封装成一个函数
(奇怪:two为什么会变成qtl)
function01.sh
#!/bin/bash
#使用function
#xie
#2018年5月19日15:46:57
function print(){
echo -e "your choice is:"
}
read -p "please input your choice:" choice
case $choice in
"one")
# tr 将小写转为大写
print; echo $choice | tr 'a-z' 'A-Z'
;;
"two")
print; echo $choice | tr 'a-z' ‘A-Z’
;;
"three")
print; echo $choice | tr 'a-z' 'A-Z'
;;
*)
echo "usage {one|two|three}"
;;
参考文献:鸟哥的linux私房菜