zoukankan      html  css  js  c++  java
  • SHELL脚本编程的条件测试

                  SHELL脚本编程的条件测试

                                             作者:尹正杰

    版权声明:原创作品,谢绝转载!否则将追究法律责任。

    一.条件测试概述 

    判断某需求是否满足,需要由测试机制来实现
      专用的测试表达式需要由测试命令辅助完成测试过程
    
    评估布尔声明,以便用在条件性执行中
      若真,则返回0
      若假,则返回1
    
    测试命令:
      test EXPRESSION
      [ EXPRESSION ]          
        #推荐使用这种风格,易读性更强,操作系统很多脚本判断都是用的这种风格,不过还是看你自己个人习惯,毕竟bash的测试命令支持这三种风格,相当灵活。
      [[ EXPRESSION ]]     
        #支持正则表达式和通配符,
        #"==","!="符号右侧可使用通配符,左侧变量名建议加双引号("")
        #"=~"符号右侧可使用正则表达式,左侧变量名建议加双引号("")
      注意:EXPRESSION前后必须有空白字符
    [root@node101.yinzhengjie.org.cn ~]# a=""
    [root@node101.yinzhengjie.org.cn ~]# test -n "$a"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# test -z "$a"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# [ -n "$a" ]             #推荐使用这种风格,易读性更强,其实操作系统的很多脚本都是使用这种风格的哟~
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# [ -z "$a" ]             
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# [[ -z "$a" ]]
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# [[ -n "$a" ]]
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    测试案例戳这里
    [root@node101.yinzhengjie.org.cn ~]# help test
    test: test [expr]
        Evaluate conditional expression.
        
        Exits with a status of 0 (true) or 1 (false) depending on
        the evaluation of EXPR.  Expressions may be unary or binary.  Unary
        expressions are often used to examine the status of a file.  There
        are string operators and numeric comparison operators as well.
        
        The behavior of test depends on the number of arguments.  Read the
        bash manual page for the complete specification.
        
        File operators:
        
          -a FILE        True if file exists.
          -b FILE        True if file is block special.
          -c FILE        True if file is character special.
          -d FILE        True if file is a directory.
          -e FILE        True if file exists.
          -f FILE        True if file exists and is a regular file.
          -g FILE        True if file is set-group-id.
          -h FILE        True if file is a symbolic link.
          -L FILE        True if file is a symbolic link.
          -k FILE        True if file has its `sticky' bit set.
          -p FILE        True if file is a named pipe.
          -r FILE        True if file is readable by you.
          -s FILE        True if file exists and is not empty.
          -S FILE        True if file is a socket.
          -t FD          True if FD is opened on a terminal.
          -u FILE        True if the file is set-user-id.
          -w FILE        True if the file is writable by you.
          -x FILE        True if the file is executable by you.
          -O FILE        True if the file is effectively owned by you.
          -G FILE        True if the file is effectively owned by your group.
          -N FILE        True if the file has been modified since it was last read.
        
          FILE1 -nt FILE2  True if file1 is newer than file2 (according to
                           modification date).
        
          FILE1 -ot FILE2  True if file1 is older than file2.
        
          FILE1 -ef FILE2  True if file1 is a hard link to file2.
        
        String operators:
        
          -z STRING      True if string is empty.
        
          -n STRING
             STRING      True if string is not empty.
        
          STRING1 = STRING2
                         True if the strings are equal.
          STRING1 != STRING2
                         True if the strings are not equal.
          STRING1 < STRING2
                         True if STRING1 sorts before STRING2 lexicographically.
          STRING1 > STRING2
                         True if STRING1 sorts after STRING2 lexicographically.
        
        Other operators:
        
          -o OPTION      True if the shell option OPTION is enabled.
          -v VAR     True if the shell variable VAR is set
          ! EXPR         True if expr is false.
          EXPR1 -a EXPR2 True if both expr1 AND expr2 are true.
          EXPR1 -o EXPR2 True if either expr1 OR expr2 is true.
        
          arg1 OP arg2   Arithmetic tests.  OP is one of -eq, -ne,
                         -lt, -le, -gt, or -ge.
        
        Arithmetic binary operators return true if ARG1 is equal, not-equal,
        less-than, less-than-or-equal, greater-than, or greater-than-or-equal
        than ARG2.
        
        Exit Status:
        Returns success if EXPR evaluates to true; fails if EXPR evaluates to
        false or an invalid argument is given.
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# help test
    [root@node101.yinzhengjie.org.cn ~]# help [
    [: [ arg... ]
        Evaluate conditional expression.
        
        This is a synonym for the "test" builtin, but the last argument must
        be a literal `]', to match the opening `['.
    [[ ... ]]: [[ expression ]]
        Execute conditional command.
        
        Returns a status of 0 or 1 depending on the evaluation of the conditional
        expression EXPRESSION.  Expressions are composed of the same primaries used
        by the `test' builtin, and may be combined using the following operators:
        
          ( EXPRESSION )    Returns the value of EXPRESSION
          ! EXPRESSION        True if EXPRESSION is false; else false
          EXPR1 && EXPR2    True if both EXPR1 and EXPR2 are true; else false
          EXPR1 || EXPR2    True if either EXPR1 or EXPR2 is true; else false
        
        When the `==' and `!=' operators are used, the string to the right of
        the operator is used as a pattern and pattern matching is performed.
        When the `=~' operator is used, the string to the right of the operator
        is matched as a regular expression.
        
        The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
        determine the expression's value.
        
        Exit Status:
    or 1 depending on value of EXPRESSION.
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# help [
    [root@node101.yinzhengjie.org.cn ~]# help [[
    [[ ... ]]: [[ expression ]]
        Execute conditional command.
        
        Returns a status of 0 or 1 depending on the evaluation of the conditional
        expression EXPRESSION.  Expressions are composed of the same primaries used
        by the `test' builtin, and may be combined using the following operators:
        
          ( EXPRESSION )    Returns the value of EXPRESSION
          ! EXPRESSION        True if EXPRESSION is false; else false
          EXPR1 && EXPR2    True if both EXPR1 and EXPR2 are true; else false
          EXPR1 || EXPR2    True if either EXPR1 or EXPR2 is true; else false
        
        When the `==' and `!=' operators are used, the string to the right of
        the operator is used as a pattern and pattern matching is performed.
        When the `=~' operator is used, the string to the right of the operator
        is matched as a regular expression.
        
        The && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
        determine the expression's value.
        
        Exit Status:
    or 1 depending on value of EXPRESSION.
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# help [[

    二.bash的数值测试(两个整数之间比较)

    -v VAR
        变量VAR是否设置
    
    数值测试:
        -gt 是否大于
        -ge 是否大于等于
        -eq 是否等于
        -ne 是否不等于
        -lt 是否小于
        -le 是否小于等于
    [root@node101.yinzhengjie.org.cn ~]# test -v age1
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# test $age1 -gt $age2
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# test $age1 -lt $age2
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test $age1 -ge $age2
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# test $age1 -le $age2
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test $age1 -eq $age2
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    测试案例戳这里
    [root@yinzhengjie ~]# [ 100 -gt 20 ] && echo "大于" || echo "no"
    大于
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# [ 100 -ge 100 ] && echo "大于等于" || echo "no"
    大于等于
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# [ 100 -lt 200 ] && echo "小于" || echo "no"
    小于
    [root@yinzhengjie ~]#
    案例展示二

     

    三.bash的字符串测试(字符串的判断) 

    字符串测试:
        = 是否等于
        > ascii码是否大于ascii码
        < 是否小于
        != 是否不等于
        =~ 左侧字符串是否能够被右侧的PATTERN所匹配(注意: 此表达式一般用于[[ ]]中;扩展的正则表达式)
        -z "STRING“ 字符串是否为空,空为真,不空为假
        -n "STRING“ 字符串是否不空,不空为真,空为假
    
    注意:用于字符串比较时的用到的操作数都应该使用引号
    [root@node101.yinzhengjie.org.cn ~]# name1="Jason Yin"
    [root@node101.yinzhengjie.org.cn ~]# name2="yinzhengjie"
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# test "$name1" = "$name2"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# test "$name1" > "$name2"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test "$name1" < "$name2"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test "$name1" != "$name2"
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]#
    [root@node101.yinzhengjie.org.cn ~]# test -z ""
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test -n ""
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# x=100
    [root@node101.yinzhengjie.org.cn ~]# test $x        #测试变量是否存在
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# unset x        #删除变量
    [root@node101.yinzhengjie.org.cn ~]# test $x
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# test -z $x      #测试变量是否是空串
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# x=""
    [root@node101.yinzhengjie.org.cn ~]# test $x
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# test -z $x      #注意哟,变量删除或者被赋值为空串都为真哟~
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# test -n $x      #我们发现若不加引号和咱们逻辑不符合,因为变量x的确是存在且其值为空。
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# test -n "$x"      #判断变量是否为不空,应该对变量加引号更加符合逻辑。
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]#
    测试案例戳这里
    [root@yinzhengjie ~]# a=yinzhengjie
    [root@yinzhengjie ~]# b=yinzhengjie
    [root@yinzhengjie ~]# [ "$a" == "$b" ] && echo yes || echo no
    yes
    [root@yinzhengjie ~]# [ "$a" == yinzhengjie ] && echo yes || echo no
    yes
    [root@yinzhengjie ~]# [ "$a" == hello ] && echo yes || echo no
    no
    [root@yinzhengjie ~]#
    案例展示二

    四.Bash的文件测试(按照文件类型进行判断) 

      玩Linux的运维童鞋都应该知道"Linux一切皆文件",我们大致将这些文件件分为七类:"块设备文件,字符设备文件,目录文件,普通文件,链接文件,管道文件,套接字文件"。
    
      存在性测试
        -a FILE:同 -e
        -e FILE: 文件存在性测试,存在为真,否则为假
    
      存在性及类别测试
        -b FILE:是否存在且为块设备文件
        -c FILE:是否存在且为字符设备文件
        -d FILE:是否存在且为目录文件
        -f FILE:是否存在且为普通文件
        -h FILE 或 -L FILE:存在且为符号链接文件
        -p FILE:是否存在且为命名管道文件
        -S FILE:是否存在且为套接字文件 
    [root@node101.yinzhengjie.org.cn ~]# cat shell/rm.sh         #编写rm.sh测试脚本案例
    #!/bin/bash
    #
    #********************************************************************
    #Author:        yinzhengjie
    #QQ:             1053419035
    #Date:             2019-11-21
    #FileName:        rm.sh
    #URL:             http://www.cnblogs.com/yinzhengjie
    #Description:        The test script
    #Copyright notice:     original works, no reprint! Otherwise, legal liability will be investigated.
    #********************************************************************
    
    #导入其它脚本中的变量,在当前脚本就不用重复定义了,直接调用被调用者的变量即可
    . /root/shell/vars
    
    [ -n "$1" ] || { echo "Usage: rm.sh file ..."; exit 10;}
    DESTDIR=/tmp/`date +%F_%T`
    [ -a "$1" ] || { echo "$1 is not exist ..."; exit 20;}
    mkdir $DESTDIR
    [ ! -d "$DESTDIR" ] && { "$DESTDIR is not directory";exit 30;}
    mv $* $DESTDIR
    echo -e "$COLOR$* is moved to $DESTDIR$COLOEREND"
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# ll shell/rm.sh 
    -rwxr-xr-x 1 root root 812 Nov 22 05:36 shell/rm.sh
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# alias rm=/root/shell/rm.sh
    [root@node101.yinzhengjie.org.cn ~]# rm /data/test.log 
    /data/test.log is moved to /tmp/2019-11-22_05:32:33
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# rm 
    Usage: rm.sh file ...
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# rm xxx
    xxx is not exist ...
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# cat shell/rm.sh         #编写rm.sh测试脚本案例
    [root@yinzhengjie ~]# test -e /etc/fstab 
    [root@yinzhengjie ~]# echo $?
    0
    [root@yinzhengjie ~]# [ -e /etc/fstab ]
    [root@yinzhengjie ~]# echo $?
    0
    [root@yinzhengjie ~]
    [root@yinzhengjie ~]# test -d /root/ && echo "yes" || echo "no"
    yes
    [root@yinzhengjie ~]# [ -d /root/ ] && echo "yes" || echo "no"
    yes
    [root@yinzhengjie ~]# 
    测试案例戳这里

    五.Bash的文件权限测试(按照文件权限进行判断)

    文件权限测试:
      -r FILE:是否存在且可读
      -w FILE: 是否存在且可写
      -x FILE: 是否存在且可执行
    
    文件特殊权限测试:
      -u FILE:是否存在且拥有suid权限
      -g FILE:是否存在且拥有sgid权限
      -k FILE:是否存在且拥有sticky权限
    [root@node101.yinzhengjie.org.cn ~/shell]# ll
    total 28
    -rw-r--r-- 1 root root 715 Nov 21 04:22 arg.sh
    -rwxr-xr-x 1 root root 549 Nov 20 07:58 backup_etc.sh
    -rwxr-xr-x 1 root root 575 Nov 21 04:47 father.sh
    -rwxr-xr-x 1 root root 812 Nov 22 05:36 rm.sh
    -rwxr-xr-x 1 root root 554 Nov 21 04:45 son.sh
    -rw-r--r-- 1 root root 476 Nov 21 05:17 test.sh
    -rw-r--r-- 1 root root  38 Nov 21 04:56 vars
    [root@node101.yinzhengjie.org.cn ~/shell]# 
    [root@node101.yinzhengjie.org.cn ~/shell]# FILE=arg.sh
    [root@node101.yinzhengjie.org.cn ~/shell]# [[ $FILE =~ .sh$ ]] && [ ! -x $FILE ] && chmod +x $FILE
    [root@node101.yinzhengjie.org.cn ~/shell]#
    [root@node101.yinzhengjie.org.cn ~/shell]# ll
    total 28
    -rwxr-xr-x 1 root root 715 Nov 21 04:22 arg.sh        #果真加上了执行权限
    -rwxr-xr-x 1 root root 549 Nov 20 07:58 backup_etc.sh
    -rwxr-xr-x 1 root root 575 Nov 21 04:47 father.sh
    -rwxr-xr-x 1 root root 812 Nov 22 05:36 rm.sh
    -rwxr-xr-x 1 root root 554 Nov 21 04:45 son.sh
    -rw-r--r-- 1 root root 476 Nov 21 05:17 test.sh
    -rw-r--r-- 1 root root  38 Nov 21 04:56 vars
    [root@node101.yinzhengjie.org.cn ~/shell]# 
    测试案例戳这里
    [root@yinzhengjie ~]# test -w yinzhengjie.txt && echo "yes" || echo "no"
    yes
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# [ -w /root/yinzhengjie.txt ] && echo yes || echo no
    yes
    [root@yinzhengjie ~]# 
    测试案例二

    六.Bash的文件属性测试(两个文件之间进行比较)

    文件大小测试:
      -s FILE: 是否存在且非空
    
    文件是否打开:
      -t fd: fd 文件描述符是否在某终端已经打开
      -N FILE:文件自从上一次被读取之后是否被修改过
      -O FILE:当前有效用户是否为文件属主
      -G FILE:当前有效用户是否为文件属组
    
    双目测试:
      FILE1 -ef FILE2: FILE1是否是FILE2的硬链接
      FILE1 -nt FILE2: FILE1是否新于FILE2(mtime)
      FILE1 -ot FILE2: FILE1是否旧于FILE2
    [root@node101.yinzhengjie.org.cn ~/shell]# ll -i
    total 32
    67254900 -rwxr-xr-x 2 root root 715 Nov 21 04:22 arg2.sh
    67254900 -rwxr-xr-x 2 root root 715 Nov 21 04:22 arg.sh
    67254899 -rwxr-xr-x 1 root root 549 Nov 20 07:58 backup_etc.sh
    67254898 -rwxr-xr-x 1 root root 575 Nov 21 04:47 father.sh
    67254903 -rwxr-xr-x 1 root root 873 Nov 22 06:10 rm.sh
    67254902 -rwxr-xr-x 1 root root 554 Nov 21 04:45 son.sh
    67254905 -rw-r--r-- 1 root root 476 Nov 21 05:17 test.sh
    67254901 -rw-r--r-- 1 root root  38 Nov 21 04:56 vars
    [root@node101.yinzhengjie.org.cn ~/shell]# 
    [root@node101.yinzhengjie.org.cn ~/shell]# FILE=arg.sh 
    [root@node101.yinzhengjie.org.cn ~/shell]# [ -t $FILE ] || echo "$FILE is open"
    arg.sh is open
    [root@node101.yinzhengjie.org.cn ~/shell]# 
    [root@node101.yinzhengjie.org.cn ~/shell]# [ arg2.sh -ef arg.sh ]
    [root@node101.yinzhengjie.org.cn ~/shell]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~/shell]# 
    测试案例戳这里
    [root@yinzhengjie ~]# ln /root/yinzhengjie.txt /tmp/test.txt
    [root@yinzhengjie ~]# ll -i /root/yinzhengjie.txt 
    651800 -rw-r--r-- 2 root root 26 Oct 11 03:39 /root/yinzhengjie.txt
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# ll -i /tmp/test.txt 
    651800 -rw-r--r-- 2 root root 26 Oct 11 03:39 /tmp/test.txt
    [root@yinzhengjie ~]# 
    [root@yinzhengjie ~]# [ /root/yinzhengjie.txt -ef /tmp/test.txt ] && echo "是硬链接" || echo "不是同一个文件"是硬链接
    [root@yinzhengjie ~]#
    案例展示二

    七.Bash的组合测试条件

    第一种方式:
      [ EXPRESSION1 -a EXPRESSION2 ] 并且
      [ EXPRESSION1 -o EXPRESSION2 ] 或者
      [ ! EXPRESSION ] 取反
      -a 和 -o 需要使用测试命令进行,[[ ]] 不支持
    
    第二种方式:
      COMMAND1 && COMMAND2 并且,短路与,代表条件性的AND THEN
      COMMAND1 || COMMAND2 或者,短路或,代表条件性的OR ELSE
      ! COMMAND 非
      示例:
        [ -f “$FILE” ] && [[ “$FILE”=~ .*.sh$ ]]
    [root@node101.yinzhengjie.org.cn ~]# x=20
    [root@node101.yinzhengjie.org.cn ~]# y=10
    [root@node101.yinzhengjie.org.cn ~]# [ $x -gt $y ]
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    0
    [root@node101.yinzhengjie.org.cn ~]# [ $x -lt $y ]
    [root@node101.yinzhengjie.org.cn ~]# echo $?
    1
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# [ $x -lt $y ] && echo "x < y"
    [root@node101.yinzhengjie.org.cn ~]# [ $x -gt $y ] && echo "x > y"
    x > y
    [root@node101.yinzhengjie.org.cn ~]# [ $x -lt $y ] && echo "x < y" || echo "x >= y"
    x >= y
    [root@node101.yinzhengjie.org.cn ~]# x=3
    [root@node101.yinzhengjie.org.cn ~]# y=5
    [root@node101.yinzhengjie.org.cn ~]# [ $x -lt $y ] && echo "x < y" || echo "x >= y"
    x < y
    [root@node101.yinzhengjie.org.cn ~]# 
    测试案例戳这里
    [root@yinzhengjie ~]# a=100
    [root@yinzhengjie ~]# [ -n "$a" -a "$a" -gt 50 ]&& echo yes || echo no
    yes
    [root@yinzhengjie ~]# a=10
    [root@yinzhengjie ~]# [ -n "$a" -a "$a" -gt 50 ]&& echo yes || echo no
    no
    [root@yinzhengjie ~]#
    案例展示二

    八.小括号"()"和花括号"{}"的区别

    "()":
        在小括号执行的指令,变量赋值和内置变量只影响小括号内的指令,而不会影响小括号外的指令。
        只是对一串命令重新开一个子shell进行执行
        最后一个命令可以不用分号
    
    "{}":
        一串命令在当前shell执行
        最后一个命令要用分号
        第一个命令和左括号之间必须要有一个空格
    
    更详细说明可参考:man bash。
    [root@node101.yinzhengjie.org.cn ~]# NAME=jason;(NAME="Jason Yin";echo $NAME);echo $NAME
    Jason Yin
    jason
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# NAME=jason;{ NAME="Jason Yin";echo $NAME;};echo $NAME
    Jason Yin
    Jason Yin
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# umask 
    0022
    [root@node101.yinzhengjie.org.cn ~]# (umask 066;touch /data/test.log)
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# touch /data/test2.log
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# ll /data/test*
    -rw-r--r-- 1 root root 0 Nov 22 05:17 /data/test2.log
    -rw------- 1 root root 0 Nov 22 05:16 /data/test.log
    [root@node101.yinzhengjie.org.cn ~]# 
    测试案例戳这里

    九.小试牛刀

    1>.编写脚本 argsnum.sh,接受一个文件路径作为参数;如果参数个数小于1,则提示用户“至少应该给一个参数”,并立即退出;如果参数个数不小于1,则显示第一个参数所指向的文件中的空白行数

    2>.写脚本 hostping.sh,接受一个主机的IPv4地址做为参数,测试是否可连通。如果能ping通,则提示用户“该IP地址可访问”;如果不可ping通,则提示用户“该IP地址不可访问”

    [root@node101.yinzhengjie.org.cn ~]# ip=127.0.0.1
    [root@node101.yinzhengjie.org.cn ~]# [[ "$ip" =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]&& echo true || echo false    #这是一个判断IP的方法。
    true
    [root@node101.yinzhengjie.org.cn ~]# 
    [root@node101.yinzhengjie.org.cn ~]# ip=127.0.0.111111
    [root@node101.yinzhengjie.org.cn ~]# [[ "$ip" =~ ^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]]&& echo true || echo false
    false
    [root@node101.yinzhengjie.org.cn ~]#

    3>.编写脚本 checkdisk.sh,检查磁盘分区空间和inode使用率,如果超过80%,就发广播警告空间将满

    4>.写脚本 per.sh,判断当前用户对指定参数文件,是否不可读并且不可写

    5>.编写脚本 excute.sh ,判断参数文件是否为sh后缀的普通文件,如果是,添加所有人可执行权限,否则提示用户非脚本文件

    6>.编写脚本 nologin.sh和 login.sh,实现禁止和允许普通用户登录系统

    [root@node101.yinzhengjie.org.cn ~]# touch /etc/nologin    #创建出改文件就可以实现禁止普通用户登录了,只要删除该文件就又允许普通用户登录啦~
  • 相关阅读:
    POST、GET请求中文参数乱码问题
    表的复制——sql语句
    mysql之limit m,n
    nullpointerxception——处理思路
    public-private-protected-默认缺省 的区别
    final关键字的作用
    使用注解来构造IOC容器
    成功的背后!(给所有IT人)
    jQuery对象复制
    键盘录入, if语句
  • 原文地址:https://www.cnblogs.com/yinzhengjie/p/7669328.html
Copyright © 2011-2022 走看看