shell 流程控制之条件判断及案例

目录

流程控制之条件判断

一,if条件语句的语法及案例

1,单分支结构

2,双分支结构

3,多分支结构

二,复合指令

三, exit退出程序

四, 多条件判断语句case


流程控制之条件判断

       条件判断语句是一种最简单的流程控制语句。该语句使得程序根据不同的条件来执行不同的程序分支。

一,if条件语句的语法

1,单分支结构

第一种语法:

if <条件表达式>

then

        指令

fi

第二种语法:

if <条件表达式>;then

      指令

fi

单分支示例1

购买英雄,如果金币大于等于6300,显示购买成功;

1,使用命令  vim condition_ctl.sh 打开脚本文件

[root@wangjingjing ~]# vim condition_ctl.sh

2,按照要求编写脚本文件

golden_corn=$1

if test $golden_corn -ge 6300

then

    echo "you got hero yasso successfully!!!"

fi 

3,使用命令  sh condition_ctl.sh 6400  执行脚本文件,结果如下:

[root@wangjingjing ~]# sh condition_ctl.sh 6400

you got hero yasso successfully!!!

单分支示例2

编写脚本,并判断当前脚本执行者,如果不是root用户,提示用户脚本需要root用户来执行,并退出。

1,使用命令  vim su_test.sh  打开脚本文件

[root@wangjingjing ~]# vim su_test.sh

2,按照要求编写脚本文件

if [[ $USER != "root" ]];then
    echo "please switch root user"
fi

3,使用命令 bash su_test.sh 执行脚本文件,结果如下:

(1)当用户为root用户时的结果:

[root@wangjingjing 0131]# bash su_test.sh

 (2)切换用户为普通用户时的结果

[root@wangjingjing 0131]# su redhat
[redhat@wangjingjing 0131]$ bash su_test.sh
please switch root user

2,双分支结构

语法:

if <条件表达式>

then

      指令序列1

else

      指令序列2

fi

双分支示例1

如果金币大于等于6300,显示购买成功,否则,显示金币不够,请充值

1,使用命令  vim condition_ctl.sh 编辑脚本文件

[root@wangjingjing 0131]# vim condition_ctl.sh

2,按照要求编辑脚本文件

golden_corn=$1

if test $golden_corn -ge 6300

then

    echo "you got hero yasso successfully!!!"

else

    echo "golden_corn not enough,please purchase failed,please rechagre!!!"

fi

3,使用命令  sh condition_ctl.sh +参数  执行脚本文件,结果如下:

[root@wangjingjing 0131]# sh condition_ctl.sh 580

golden_corn not enough,please purchase failed,please rechagre!!!

[root@wangjingjing 0131]# sh condition_ctl.sh 78000

you got hero yasso successfully!!!

双分支示例2

判断 sshd 进程是否运行,如果服务未启动则启动相应服务。

1,使用命令  vim ssh_stat.sh  打开脚本文件

[root@wangjingjing 0131]# vim ssh_stat.sh

2,按照要求编辑脚本文件

tips:

-v  反选

wc 统计

#找到sshd的相关进程并统计数量

process_count=`ps -ef | grep sshd | grep -v grep | wc -l`

if [ $process_count -ge 1 ]

then

    echo "service sshd is running"

else

    echo "service sshd is stopped"

# 把输出存放到/dev/null中

    systemctl start sshd >/dev/null

    sleep 3

fi

3,使用命令 bash ssh_stat.sh 执行脚本文件,结果如下:

[root@wangjingjing 0131]# bash ssh_stat.sh

service sshd is running

3,多分支结构

语法:

if 条件表达式1

then

       命令序列1

elif 条件表达式2

then

       命令序列2

elif 条件表达式3

then

       命令序列3

else                                        (    此处的else 可有可无   )

       命令序列n

Fi

       在上面的语法中,当整个if elif语句结构中的第1个条件表达式为真,则执行第1个then子句中的语句 statement1;否则,继续下面的判断。如果条件表达式2的值为真,则执行第2个then子句中的语句,以此类推。如果所有的条件表达式的值都为假,则执行最后的else子句中的语句。最后是if elif结构的结束 标志fi。

多分支示例1

判定一个成绩:

85-100 -》 A

70-84 -> B

60-69 -> C

0-59 ->D

程序的边界问题:>100, <0

必须输入的是数字:0-100

假设:输入成绩的人是可靠的,输入的成绩都是有效的

1,使用命令 vim score_script.sh 编辑脚本文件

[root@wangjingjing 0131]# vim score_script.sh

2,按照要求编辑脚本文件

score=$1

if [ $score -ge 85 -a $score -le 100 ]

then

    echo "LEVEL A"

elif [ $score -ge 70 -a $score -lt 85 ]

then

    echo "LEVEL B"

elif [ $score -ge 60 -a $score -lt 70 ]

then

    echo "LEVEL C"

elif [ $score -ge 0 -a $score -lt 60 ]

then

    echo "LEVEL D"

else

    echo "wrong score"

fi

3,使用命令  bash score_script.sh +参数  执行脚本文件,结果如下:

[root@wangjingjing 0131]# bash score_script.sh 100

LEVEL A

[root@wangjingjing 0131]# bash score_script.sh 76

LEVEL B

[root@wangjingjing 0131]# bash score_script.sh 65

LEVEL C

[root@wangjingjing 0131]# bash score_script.sh 26

LEVEL D

[root@wangjingjing 0131]# bash score_script.sh 101

wrong score

[root@wangjingjing 0131]# bash score_script.sh -1

wrong score

多分支示例2:

判断cpu是那个类型的

tips:

[[]] -> 支持正则表达式匹配: =~

查看主机的cpu生产商:cat /proc/cpuinfo

1,使用命令 vim cpu_script.sh 编辑脚本文件

[root@wangjingjing 0131]# vim cpu_script.sh

2,按照要求编辑脚本文件

#在文件/proc/cpuinfo 中找到唯一一个vendr_id

cpu_type=`grep "vendor_id" /proc/cpuinfo | uniq`

#使用正则表达式匹配到AuthenticAMD

if [[ $cpu_type =~ AuthenticAMD ]]

then

    echo "CPU Type is AMD"

#使用正则表达式匹配到GenuineIntel   

elif [[ $cpu_type =~ GenuineIntel ]]

then

    echo "CPU Type is Intel"

else

    echo "CPU Type is other"

fi

3,使用命令 bash cpu_script.sh 执行脚本文件,结果如下:

[root@wangjingjing 0131]# bash cpu_script.sh

CPU Type is Intel

 

二,复合指令

复合指令:即一串命令。

()和{}都是对一串的命令进行执行,但有所区别:

相同点:

        ()和{}都是把一串的命令放在括号里面,如果命令在一行命令之间用;号隔开;

                 ()和{}括号里面某个命令的重定向只影响该命令,但括号外的重定向则

                 会被括号里的所有命令影响。

不同点

()只是对一串命令重新开一个子shell进行执行,{}对一串命令在当前shell执行;

     ()最后一个命令可以不用分号,{}最后一个命令要用分号;

     ()里的第一个命令和左边括号不必有空格,{}的第一个命令和左括号之间必须要有一个空格。

举例:

()和{}都是把一串的命令放在括号里面,如果命令在一行命令之间用;号隔开;

()最后一个命令可以不用分号,{}最后一个命令要用分号

()里的第一个命令和左边括号不必有空格,{}的第一个命令和左括号之间必须要有一个空格

{}注意空格和分号

[root@wangjingjing 0131]# { pwd;ls;echo "123";}

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test.sh      ssh_stat.sh

44  cpu_script.sh     file1  num_test2.sh  score_script.sh  su_test.sh

123

()

[root@wangjingjing 0131]# (pwd;ls;echo "123")

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test.sh      ssh_stat.sh

44  cpu_script.sh     file1  num_test2.sh  score_script.sh  su_test.sh

123

  

()多行书写:

[root@wangjingjing 0131]# (pwd

> ls

> echo "123"

> )

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test.sh      ssh_stat.sh

44  cpu_script.sh     file1  num_test2.sh  score_script.sh  su_test.sh

123

{ }多行书写:       注意开始的空格

[root@wangjingjing 0131]# { pwd

> ls

> echo "123"

> }

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test.sh      ssh_stat.sh

44  cpu_script.sh     file1  num_test2.sh  score_script.sh  su_test.sh

123

()和{}括号里面某个命令的重定向只影响该命令,但括号外的重定向则会被括号里的所有命令影响。

括号里面使用

[root@wangjingjing 0131]# { pwd;ls;echo "123" > file.txt; }

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test.sh      ssh_stat.sh

44  cpu_script.sh     file1  num_test2.sh  score_script.sh  su_test.sh

[root@wangjingjing 0131]# more file.txt

123

括号外面使用:

[root@wangjingjing 0131]# { pwd;ls;echo "123"; } > file.txt

[root@wangjingjing 0131]# more file.txt

/root/shell_code23/0131

23

44

condition_ctl.sh

cpu_script.sh

dir1

file1

file_test.sh

file.txt

num_test2.sh

num_test.sh

score_script.sh

ssh_stat.sh

su_test.sh

123

()只是对一串命令重新开一个子shell进行执行,{}对一串命令在当前shell执行;

小括号:

[root@wangjingjing 0131]# (pwd;ls;echo "123";sleep 200;)

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test2.sh  score_script.sh  su_test.sh

44  cpu_script.sh     file1  file.txt      num_test.sh   ssh_stat.sh

123

 

 

大括号:

[root@wangjingjing 0131]# { pwd;ls;echo "123";sleep 200; }

/root/shell_code23/0131

23  condition_ctl.sh  dir1   file_test.sh  num_test2.sh  score_script.sh  su_test.sh

44  cpu_script.sh     file1  file.txt      num_test.sh   ssh_stat.sh

123

 

 

 

 

三, exit退出程序

        exit语句的基本作用是终止Shell程序的执行。除此之外,exit语句还可以带一个可选的参数,用来指定程 序退出时的状态码。

exit语句的基本语法如下:

 exit status

        其中,status参数表示退出状态,该参数是一个整数值,其取值范围为0~255。与其他的Shell命令一 样,Shell程序的退出状态也储存在系统变量$?中,因此,用户可以通过该变量取得Shell程序返回给父进 程的退出状态码

四, 多条件判断语句case

case语句语法:

case 变量名 in

     值1)

        指令1

      ;;

     值2)

        指令2

      ;;

     值3)

        指令3

      ;;

        *)

          默认

esac

        case语句会将该变量的值与每个值相比较,如果与某个值相等,则执行该value所对应的一组语句。当遇 到“;;”符号时,就跳出case语句,执行esac语句后面的语句。如果没有任何一个值与variable的值相匹 配,则执行*后面的一组语句。

示例1

输入1,打印1,输入2,打印2……

1,使用命令 vim case_script.sh 编辑脚本文件

[root@wangjingjing 0131]# vim case_script.sh

2,按照要求编辑脚本文件

num=$1

case $num in

    1)

        echo "1"

    ;; 

    2)

        echo "2"

    ;; 

    3)

        echo "3"

    ;; 

    4)

        echo "4"

    ;; 

    *)

        echo "other number"

    ;;

esac

3,使用命令  bash case_script.sh+参数  执行脚本文件,结果如下:

[root@wangjingjing 0131]# bash case_script.sh 1

1

[root@wangjingjing 0131]# bash case_script.sh 2

2

[root@wangjingjing 0131]# bash case_script.sh 3

3

[root@wangjingjing 0131]# bash case_script.sh 4

4

[root@wangjingjing 0131]# bash case_script.sh 6

other number

示例2

由用户从键盘输入一个字符,并判断该字符是否为字母、数字或者其他字符, 并输出相应的提 示信息。

1,使用命令 vim case_script2.sh 编辑脚本文件

[root@wangjingjing 0131]# vim case_script2.sh

2,按照要求编辑脚本文件

char=$1

case $char in

    [a-z] | [A-Z])

        echo "letter"

    ;; 

    [0-9])

        echo "number"

    ;; 

    *)

        echo "other char"

esac

3,使用命令  bash case_script2.sh+参数  执行脚本文件,结果如下:

[root@wangjingjing 0131]# bash case_script2.sh 1

number

[root@wangjingjing 0131]# bash case_script2.sh a

letter

[root@wangjingjing 0131]# bash case_script2.sh A

letter

[root@wangjingjing 0131]# bash case_script2.sh #

other char

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值