shell脚本之多分支条件判断语句

多分支if语句语法:

if [ 条件判断式1 ]

then

当条件判断式1成立时,执行程序1

elif [ 条件判断式2 ]

then

当条件判断式2成立时,执行程序2

........

else

当所有条件都不成立时,最后执行此程序

fi

例子:判断输入的文件名是什么文件

#!/bin/bash

read -t 30 -p "please input a filename:" file

if [ -z "$file" ]

      then 

           echo " please enter the content  "

          exit 11

elif [ ! -e "$file" ]

      then 

         echo "please enter the file name"

         exit 12

elif   [ -f "$file" ]

           then

           echo "$file is common file"

elif [ -d "$file" ]

           then 

           echo "$file is dir file"

else 

         echo "$file is other file"

fi 

case 判断语句语法:

case $变量名 in

“值1”)        #值需要用双引号引用

如果变量的值等于值1,则执行程序1

  ;;

“值2”)

如果变量的值等于值2,则执行程序2

   ;;

....省略其他分支.....

*)

  如果以上值不是,执行一下程序

   ;;

esac

注意:每个分支已两个分号;;结尾,整个语句以esac结尾;比较的值需要用""引用

例子:大学生毕业的归宿选择

1:去企业找工作

2:自己创业

3:考研

#!/bin/bash

echo "1: 去企业找工作"

echo "2:自己创业"

echo "3:考研"

read -t 30 -p "please enter your post-college choice" $choice

case $choice in

             “1”)

                  echo "Look for a job in a company"

               ;;

             "2")

                  echo "start my own business"

                ;;

             "3")

                  echo "kake part in the postgraduate entrance exams"

                ;;

               *)

                   ehco "other"

                  ;;

esac

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值