zoukankan      html  css  js  c++  java
  • shell-添加条件测试的多种方法语法介绍与简单实战

    1. 条件测试
      1) 测试语句
      1) 条件测试语法
        在bash的各种流程控制结构中通常要进行各种测试,然后根据测试结果执行不同的操作,有时也会通过与if等条件语句相结合,使我们可以方便的完成判断。
      2) 【语法说明】
        格式1:test<测试表达式>
        格式2:[<测试表达式>]
        格式3:[[<测试表达式>]]
        说明:
          格式1和格式2是等价的。
        格式3为扩展的test命令,有网友推荐用推荐3,老男孩习惯使用格式2.
        提示:
          在[[]]中可以使用通配符进行模式匹配。&&、||、>、<等操作符可以应用于[[]]中,但不能应用于[]中。
          对整数进行关系运算,也可以使用shell的算术运算符(())。
      3) 【语法例子】
        格式1:test<测试表达式>
        范例1:test测试文件

    [root@1-241 scripts]# test -f file && echo true ||echo false
    false
    [root@1-241 scripts]# touch file
    [root@1-241 scripts]# test -f file && echo true ||echo false
    true
    

        范例2:test命令非(!)的写法

    [root@1-241 scripts]# test ! -f file && echo true ||echo false
    true
    [root@1-241 scripts]# touch file
    [root@1-241 scripts]# test ! -f file && echo true ||echo false
    false
    

        格式2:[<测试表达式>]
        范例:[]

    [root@1-241 scripts]# [ -f file ] && echo true ||echo false
    false
    [root@1-241 scripts]# touch file
    [root@1-241 scripts]# [ -f file ] && echo true ||echo false
    true
    [root@1-241 scripts]# [ -f file ] && cat file
    [root@1-241 scripts]# rm -rf file
    [root@1-241 scripts]# [ -f file ] && cat file
    [root@1-241 scripts]# cat file
    cat: file: 没有那个文件或目录
    

        格式3:[[<测试表达式>]]

        范例:[[]]

    [root@1-241 scripts]# [[ -f file ]] && echo true || echo false
    false
    [root@1-241 scripts]# [[ ! -f file ]] && echo true || echo false
    true
    [root@1-241 scripts]# [[ -f file && -f folder ]] && echo true || echo false
    false
    [root@1-241 scripts]# [ -f file && -f folder ] && echo true || echo false
    -bash: [: missing `]'
    false
    [root@1-241 scripts]# [ -f file -a -f folder ] && echo true || echo false
    false
    [root@1-241 scripts]# touch file folder
    [root@1-241 scripts]# [ -f file -a -f folder ] && echo true || echo false
    true
    [root@1-241 scripts]# 
    [root@1-241 scripts]# rm -rf file
    [root@1-241 scripts]# [ -f file -a -f folder ] && echo true || echo false
    false
    

    2. 文件测试操作符
      在书写测试表达式时,可以使用表1.3.4中的文件测试操作符
      表1.3.4常用文件测试操作符号

       常用文件测试操作符号的记忆方法:

    [root@1-241 scripts]# echo f=file
    f=file
    [root@1-241 scripts]# echo d=dirctory
    d=dirctory
    [root@1-241 scripts]# echo s=size
    s=size
    [root@1-241 scripts]# echo e=exist
    e=exist
    [root@1-241 scripts]# echo r=read
    r=read
    [root@1-241 scripts]# echo w=write
    w=write
    [root@1-241 scripts]# echo x=executable
    x=executable
    [root@1-241 scripts]# echo nt="new than"
    nt=new than
    [root@1-241 scripts]# echo ot="old than"
    ot=old than
    

      

     

  • 相关阅读:
    SAP OPEN UI5 Step 8: Translatable Texts
    SAP OPEN UI5 Step7 JSON Model
    SAP OPEN UI5 Step6 Modules
    SAP OPEN UI5 Step5 Controllers
    SAP OPEN UI5 Step4 Xml View
    SAP OPEN UI5 Step3 Controls
    SAP OPEN UI5 Step2 Bootstrap
    SAP OPEN UI5 Step1 环境安装和hello world
    2021php最新composer的使用攻略
    Php使用gzdeflate和ZLIB_ENCODING_DEFLATE结果gzinflate报data error
  • 原文地址:https://www.cnblogs.com/scajy/p/12765636.html
Copyright © 2011-2022 走看看