设为首页 收藏本站
查看: 936|回复: 0

shell编程之选择结构

[复制链接]
YunVN网友  发表于 2018-8-22 12:33:27 |阅读模式
数值比较:  
                   -eq               等于
  
                   -gt               大于
  
                   -lt               小于
  
                   -ge               大于等于
  
                   -le               小于等于
  
                   -ne               不等于
  

  
                   脚本实例:
  
                            #!/bin/bash
  
                            #
  
                            var1=10
  
                            var2=11
  

  
                            if [ $var1 -gt 5 ];then
  
                                 echo "The test value $var1 is greater than 5."
  
                            fi
  

  
                            if [ $var1 -eq $var2 ];then
  
                                echo "The values are equal."
  
                            else
  
                                echo "The values are different."
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 21.sh
  
                            [root@localhost ~]# ./21.sh
  
                            The test value 10 is greater than 5.
  
                            The values are different.
  

  
                            #!/bin/bash
  
                            #
  
                            var1=`echo "scale=4;10 / 3"| bc`
  
                            echo "The test value is $var1"
  
                            if [ $var1 -gt 3 ];then
  
                                echo "The result is larger than 3"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 24.sh
  
                            [root@localhost ~]# ./24.sh
  
                            The test value is 3.3333
  
                            ./24.sh: line 5: [: 3.3333: integer expression expected
  

  
                            //看到了吗?test命令只支持整数
  
                            //bash只能处理整数数字。使用bc时,只是欺骗bash将浮点值
  
                            作为字符串存储在一个变量中。
  

  
        字符串比较:
  
                   =                 是否相等
  
                   !=                是否不等
  
                   <                 是否小于
  
                   >                 是否大于
  
                   -n str1           检查str1的长度是否大于0  非空(true)  空(false)
  
                   -z str1           检查str1的长度是否为0    空(true)  非空(false)
  

  
                   脚本实例:
  
                             判断当前用户是否为root
  
                            #!/bin/bash
  
                            #
  
                            testuser=root
  
                            if [ $USER = $testuser ]
  
                            then
  
                                echo "Welcome $testuser"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 25.sh
  
                            [root@localhost ~]# whoami
  
                            root
  
                            [root@localhost ~]# ./25.sh
  
                            Welcome root
  

  
                            #!/bin/bash
  
                            #
  
                            testuser=baduser
  
                            if [ $USER != $testuser ];then
  
                                echo "This isn't $testuser"
  
                            else
  
                                echo "Welcome $testuser"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 26.sh
  
                            [root@localhost ~]# whoami
  
                            root
  
                            [root@localhost ~]# ./26.sh
  
                            This isn't baduser
  

  

  
                            #!/bin/bash
  
                            #
  
                            var1=baseball
  
                            var2=hockey
  
                            if [ $var1 \> $var2 ];then
  
                                echo "$var1 is greater than $var2"
  
                            else
  
                                echo "$var1 is less than $var2"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 27.sh
  
                            [root@localhost ~]# ./27.sh
  
                            baseball is less than hockey
  

  
                            注:字符的大小比较是比较的字符串的ASCII码大小
  
                                >和 $var2 ];then
  
                                echo "$var1 is greater than $var2"
  
                            else
  
                                echo "$var1 is less than $var2"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 28.sh
  
                            [root@localhost ~]# ./28.sh
  
                            Testing is less than testing
  

  
                            判断一个变量是否包含数据:
  
                            #!/bin/bash
  
                            #
  
                            var1=testing
  
                            var2=''
  
                            if [ -n $var1 ];then
  
                               echo "The string $var1 is not empty"
  
                            else
  
                               echo "The string $var1 is empty"
  
                            fi
  

  
                            if [ -z $var2 ];then
  
                               echo "The string $var2 is empty"
  
                            else
  
                               echo "The string $var2 is not empty"
  
                            fi
  

  
                            if [ -z $var3 ];then
  
                               echo "The string $var3 is empty"
  
                            else
  
                               echo "The string $var3 is not empty"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 30.sh
  
                            [root@localhost ~]# ./30.sh
  
                            The string testing is not empty
  
                            The string  is empty
  
                            The string  is empty
  

  
                            注:空变量和没有初始化的变量可能会对shell脚本测试
  
                            产生灾难性的影响。如果不确定变量的内容,在数值比较
  
                            或者字符串比较中使用它之前,最好使用-n或者-z测试下
  
                            它是否有值。
  

  
         文件比较:
  
                  -d file            检查file是否存在并且是一个目录
  
                  -e file            检查file是否存在
  
                  -f file            检查file是否存在并且是一个文件
  
                  -r file            检查file是否存在并且可读
  
                  -s file            检查file是否存在并且不为空
  
                  -w file            检查file是否存在并且可写
  
                  -x file            检查file是否存在并且可执行
  
                  -O file            检查file是否存在并且被当前用户拥有
  
                  -G file            检查file是否存在并且默认组是否为当前用户组
  
                  file1 -nt file2    检查file1是否比file2新
  
                  file1 -ot file2    检查file1是否比file2旧
  

  
                  脚本实例:
  
                            判断文件夹是否存在:
  
                            #!/bin/bash
  
                            #
  
                            if [ -d $HOME ];then
  
                                echo "Your HOME directory exists"
  
                                cd $HOME
  
                            else
  
                                echo "There's a problem with your HOME directory"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 31.sh
  
                            [root@localhost ~]# ./31.sh
  
                            Your HOME directory exists
  

  
                            如果无法判断是文件或文件夹,可以使用-e来判断
  
                            #!/bin/bash
  
                            #
  
                            if [ -e $HOME ];then
  
                                echo "OK on the directory,now let's check the file"
  
                                if [ -e $HOME/testing ];then
  
                                     echo "Appending date to existing file"
  
                                     date >>$HOME/testing
  
                                else
  
                                     echo "Creating new file"
  
                                     date >>$HOME/testing
  
                                fi
  
                            else
  
                                echo "Sorry,you don't have a HOME directory"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 32.sh
  
                            [root@localhost ~]# ./32.sh
  
                            OK on the directory,now let's check the file
  
                            Creating new file
  
                            [root@localhost ~]# cat testing
  
                            Thu Jun 12 11:39:26 PDT 2014
  

  
                            -e既适用于文件也适用于目录
  
                            -f只适用于文件
  
                            -d只适用于目录
  
                            #!/bin/bash
  
                            #
  
                            if [ -e $HOME ];then
  
                                echo "The object exists,is it a file?"
  
                                if [ -f $HOME ];then
  
                                       echo "Yes,it's a file"
  
                                else
  
                                       echo "No,it's not a file"
  
                                       if [ -f $HOME/.bash_history ];then
  
                                             echo "But this is a file"
  
                                       fi
  
                                fi
  
                            else
  
                                echo "Sorry,the object doesn't exists"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 33.sh
  
                            [root@localhost ~]# ./33.sh
  
                            The object exists,is it a file?
  
                            No,it's not a file
  
                            But this is a file
  

  
                            在尝试从文件中读取数据之前,通常检查是否能够读取改文件
  
                            #!/bin/bash
  
                            #
  
                            pwfile=/etc/shadow
  
                            if [ -f $pwfile ];then
  
                                if [ -r $pwfile ];then
  
                                    tail $pwfile
  
                                else
  
                                    echo "Sorry,I'm unable to read the $pwfile file"
  
                                fi
  
                            else
  
                                echo "Sorry,the file $pwfile doesn't exist"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 34.sh
  
                            [root@localhost ~]# ./34.sh
  
                            ntp:!!:16203::::::
  
                            apache:!!:16203::::::
  
                            saslauth:!!:16203::::::
  
                            postfix:!!:16203::::::
  
                            pulse:!!:16203::::::
  
                            sshd:!!:16203::::::
  
                            tcpdump:!!:16203::::::
  
                            admin:$1$ThVHQoOH$mf66dh2AU.pZh1ky0olou1:16203:0:99999:7:::
  
                            student:!!:16229:0:99999:7:::
  
                            student1:!!:16229:0:99999:7:::
  

  
                            判断文件是否为空
  
                            #!/bin/bash
  
                            #
  
                            file=t1test
  
                            touch $file
  
                            if [ -s $file ];then
  
                               echo "The $file exists and has data in it"
  
                            else
  
                               echo "The $file not exists or is empty"
  
                               date >>$file
  
                            fi
  

  
                            if [ -s $file ];then
  
                               echo "The $file file has data in it"
  
                            else
  
                               echo "The $file file is still empty"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 35.sh
  
                            [root@localhost ~]# ./35.sh
  
                            The t1test not exists or is empty
  
                            The t1test file has data in it
  

  
                            判断文件是否可写
  
                            #!/bin/bash
  
                            #
  
                            logfile=$HOME/t1test
  
                            touch $logfile
  
                            chmod u-w $logfile
  
                            now=`date +%Y%m%d-%H%M`
  

  
                            if [ -w $logfile ];then
  
                                echo "The program run at: $now" >$logfile
  
                                echo "The first attemp successed"
  
                            else
  
                                echo "The first attemp failed"
  
                            fi
  

  
                            chmod u+w $logfile
  
                            if [ -w $logfile ];then
  
                               echo "The program run at: $now" >$logfile
  
                               echo "The second attemp successed"
  
                            else
  
                               echo "The second attemp failed"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# ./36.sh
  
                            The first attemp failed
  
                            The second attemp successed
  

  
                            判断文件是否能运行
  
                            #!/bin/bash
  
                            #
  
                            if [ -x 22.sh ];then
  
                               echo "You can run the script."
  
                               ./22.sh
  
                            else
  
                               echo "Sorry,you are unable to execute the script."
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 37.sh
  
                            [root@localhost ~]# ./37.sh
  
                            You can run the script.
  

  
                            判断文件所有者是否为当前用户
  
                            #!/bin/bash
  
                            #
  
                            if [ -O /etc/passwd ];then
  
                                echo "You're the owner of the /etc/passwd file"
  
                            else
  
                                echo "You're not the owner of the /etc/passwd file"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# ./38.sh
  
                            You're the owner of the /etc/passwd file
  

  
                            判断文件属组是否与当前用户的默认组相同
  
                            #!/bin/bash
  
                            #
  
                            if [ -G $HOME/testing ];then
  
                               echo "Same group"
  
                            else
  
                               echo "Not same group"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# ./39.sh
  
                            Same group
  

  
                            判断文件新旧
  
                            #!/bin/bash
  
                            #
  
                            if [ ./22.sh -nt ./39.sh ];then
  
                               echo "22.sh new"
  
                            else
  
                               echo "39.sh new"
  
                            fi
  

  
                            if [ ./22.sh -ot ./39.sh ];then
  
                               echo "22.sh old"
  
                            else
  
                               echo "39.sh old"
  
                            fi
  

  
                            执行结果:
  
                            [root@localhost ~]# chmod +x 40.sh
  
                            [root@localhost ~]# ./40.sh
  
                            39.sh new
  
                            22.sh old
  

  
                            注:-nt -ot 不能判断文件是否存在
  
                               如果文件不存在则返回flase



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-555134-1-1.html 上篇帖子: linux学习之shell练习 下篇帖子: shell脚本1
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表