• shell 条件测试


    格式1:test 条件表达式

    格式2:[条件表达式]

    格式3:[[条件表达式]]

    man test

    [root@localhost ~]# test -d /home
    [root@localhost ~]# echo $?
    0
    [root@localhost ~]# [ -d /home ]
    [root@localhost ~]# echo $?
    0
    [root@localhost ~]# test -d /home111
    [root@localhost ~]# echo $?
    1
    [root@localhost ~]# [ -d /home ];echo $?
    0
    [root@localhost ~]# [[ -d /home ]];echo $?   #2个括号支持正则表达式
    0
    [root@localhost ~]# [[[ -d /home ]]];echo $?
    -bash: [[[: command not found
    127

    [ -e dir|file ]

    [ -d dir ]

    [ -f file ]

    [ -r file ] 当前用户对该文件是否有读权限

    [ -x file ]当前用户对该文件是否有执行权限

    [root@localhost ~]# [ -e /home ];echo $?
    0
    [root@localhost ~]# [ -f /home ];echo $?
    1
    [root@localhost ~]# [ -f /etc/passwd ];echo $?
    0
    [root@localhost ~]# touch /file1
    [root@localhost ~]# chmod u+x /file1 
    [root@localhost ~]# ll /file1
    -rwxr--r-- 1 root root 0 Jan  4 14:32 /file1
    [root@localhost ~]# [ -x file1 ];echo $?
    1
    [root@localhost ~]# su - wwb
    [wwb@localhost ~]$ [ -x /file1 ];echo $?
    1
    [wwb@localhost ~]$ [ -L /etc/passwd ]; echo $?
    1
    [wwb@localhost ~]$ [ ! -L /etc/passwd ]; echo $?
    0
    #!/usr/bin/env bash
    
    for ip in `cat $1`
    do
            ping -c1 $ip &>/dev/null
            if [ $? -eq 0 ]; then
                    echo "$ip is up"
            else
                    echo "$ip is down"
            fi
    done

    不同的错误定义不同的返回值

    [root@localhost ~]# cat ping03.sh 
    #!/usr/bin/env bash
    
    if [ $# -eq 0 ];then
        echo "usage:`basename $0` file"
        exit 1
    fi
    
    if [ ! -f $1 ];then    
        echo "error please use file"
        exit 2
    fi
    
    for ip in `cat $1`
    do
        ping -c1 $ip &>/dev/null
        if [ $? -eq 0 ];then
            echo "$ip:up"
        else
            echo "$ip:down"
        fi
    done

     数值比较[ 整数1 操作符 整数2 ]

    [ 1 -gt 10 ]:大于

    [ 1 -lt 10  ] :小于

    [ 1 -eq 10 ]:等于

    [ 1 -ne 10 ]:不等于

    [ 1 -ge 10 ]:大于等于

    [ 1 -le 10  ]:小于等于

    [root@localhost ~]# disk_use=`df -Ph | grep '/$'| awk '{print $5}' |awk -F'%' '{print $1}'`
    [root@localhost ~]# [ $disk_use -gt 60 ]&&echo "warning.."
    [root@localhost ~]# [ $disk_use -gt 30 ]&&echo "warning.."
    warning..
    [root@localhost ~]# [ $uid -eq 0 ] && echo "The current user is the super user" || echo "The current user is not super user"
    The current user is the super user

    磁盘使用率

    #!/usr/bin/env bash
    #disk use
    #v1.0
    
    #disk_use=`df -Ph | grep /$ | awk '{print $5}'|awk -F'%' '{print $1}'`
    #disk_name=`df -Ph | grep /$ | awk '{print $6}'`
    IFS=$'
    '  #for默认空格最为换行改成回车作为换行
    for  line in  `df -Ph|grep -v Size`
    do
            {
            disk_use=`echo $line|awk '{print $5}'|awk -F'%' '{print $1}'`
            disk_name=`echo $line| awk '{print $6}'`
    
            if [ $disk_use -gt 0 ];then
                    echo "`date +%F` $disk_name: ${disk_use}%"
            fi
            }
    
    done
    #!/usr/bin/env bash
    #mem use
    #v1.0 by wwb
    
    mem_total=`free -m | grep Mem | awk '{print $2}'`
    mem_used=`free -m | grep Mem | awk '{print $3}'`
    mem_per=$((mem_used*100/mem_total))
    
    war_mem_file=/tmp/war_mem.txt
    
    if [ $mem_per -gt 80 ];then
            echo "`date +%F-%M` memory:${mem_per}%" |tee $war_mem_file
    fi
    
    if [ -f $war_mem_file ];then
            mail -s "mem warning" root <$war_mem_file
            rm -rf $war_mem_file
    fi

    字符串比较

    提示:使用双引号

    [root@localhost ~]# [ "$USER" = "root" ];echo $?
    0
    [root@localhost ~]# [ "$USER" = "wwb" ];echo $?
    1
    [root@localhost ~]# [ "$USER" =~ ^ro ];echo $?
    -bash: [: =~: binary operator expected
    2
    [root@localhost ~]# [[ "$USER" =~ ^ro ]];echo $?
    0
    [root@localhost ~]# ./useradd.sh 
    Please input number0k
    error number
    [root@localhost ~]# ./useradd.sh 
    Please input number10
    Please input prefix:wwb
    wwb1 create ok
    wwb2 create ok
    wwb3 create ok
    wwb4 create ok
    wwb5 create ok
    wwb6 create ok
    wwb7 create ok
    wwb8 create ok
    wwb9 create ok
    wwb10 create ok
    [root@localhost ~]# cat useradd.sh 
    #!/usr/bin/env bash
    #useradd
    #v1.0 by wwb 14/4/2017
    
    read -p "Please input number" num
    if [[ ! $num =~ ^[0-9]+$ ]];then
        echo "error number"
        exit 1
    fi
    
    read -p "Please input prefix:" prefix
    
    for i in `seq $num`
    do
        user=${prefix}${i}
        useradd $user
        if [ $? -eq 0 ];then
            echo "$user create ok"
        fi
    done
  • 相关阅读:
    [转]你每天90%的注意力被浪费了
    通过浏览器navigator获取当前经纬度位置
    Sublime Text 3 安装、插件与个人配置
    ios判断手机号是否可用
    AFNetworking提示3840 Unescaped control character around character XXX
    理解OC“属性”这一概念
    熟悉Objective—C
    如何在自己的应用中打开第三方应用程序
    ios多线程开发总结
    UIView的Touch事件UIControlEvents详解
  • 原文地址:https://www.cnblogs.com/weiwenbo/p/6701623.html
Copyright © 2020-2023  润新知