利用变量编写几个简单的脚本

  • 执行脚本查看用户(DUSHAN)如存在则提示用户存在并退出,如不存在自动创建并提示创建成功,密码设为123456

    #!/bin/bash
    
    #*************************************************************************
    #Author:                 Dadda_Du
    #QQ:                     316722220     
    #Date:                   2018-08-04    
    #FileName:               idname_60.sh
    #URL:                     https://blog.csdn.net/weixin_40001704
    #Description:             command idname test user exist?
    #Copyright(C):            2018 all rights reserved
    #*************************************************************************
    name=dushan
    
    id $name &>/dev/null && { echo $name is exist!;exit; } || { useradd $name ; echo $name is create!; } && ( `echo 123456 | passwd --stdin
    $name &>/dev/null` )  
    

(注意:括号里每个单词之间注意空格;邮箱路径/var/spool/mail/用户名;家目录路径/home/用户名)


  • 执行此脚后磁盘大于66%发送广播并同时报送当前INODE使用情况

    #!/bin/bash  
    
    #*************************************************************************
    #Author:                 Dadda_Du
    #QQ:                     316722220      
    #Date:                   2018-08-04    
    #FileName:               checkdisk_60.sh
    #URL:                     https://blog.csdn.net/weixin_40001704
    #Description:             check the disk full?
    #Copyright(C):            2018 all rights reserved      
    #*************************************************************************
    Diskuse=`df|grep /dev/sd|egrep -o '[0-9]{1,3}%'|egrep -o '[0-9]{1,3}'|sort -nr|head -n1`
    InodeNum=`df -i|egrep -o '[0-9]{1,3}%'|egrep -o '[0-9]{1,3}'|sort -nr|head -n1`
    n=1
    
    [ $Diskuse -gt $n ] && wall "Disk use >(gt) $n%"
    [ $InodeNum -eq $n ] && wall "Inode use eq $n%"
    

    (注意:中括号与内容间空格,中括号内变量添加双引号,避免报错)


  • 执行此脚本后判断系统版本

    #!/bin/bash           
    
    #*************************************************************************
    #Author:                 Dadda_Du
    #QQ:                     316722220     
    #Date:                   2018-08-04    
    #FileName:               checkversion_60.sh
    #URL:                     https://blog.csdn.net/weixin_40001704
    #Description:             Checkversion 6 or 7?
    #Copyright(C):            2018 all rights reserved
    #*************************************************************************
    ver=` egrep -o "[0-9]+" /etc/redhat-release|head -n1`
    
    [ "$ver" -eq 7 ] && echo "Centos7" || echo "Centos6"
    

  • 执行此脚本后可判断键入内容(191.191.111.111)是否为合法IP

    #!/bin/bash  
    
    #*************************************************************************
    #Author:                 Dadda_Du
    #QQ:                     316722220     
    #Date:                   2018-08-04    
    #FileName:               checkip_60.sh
    #URL:                     https://blog.csdn.net/weixin_40001704
    #Description:             Legal IP?
    #Copyright(C):            2018 all rights reserved
    #*************************************************************************
    IP=191.191.111.111
    
    [[ "$IP" =~ ^(([1-9]?[0-9]|1[0-9]?[0-9]|2[0-4][0-9]|25[0-5])\.){3}([1-9]?[0-9]|1[0-9]?[0-9]|2[0-4][0-9]|25[0-5])$ ]] && echo "The $IP 
    is legal!" || echo "The $IP is illegal!!"
    

    (注意:中括号中的变量要加双引号,正则表达式后面判断内容不要加双引号)


猜你喜欢

转载自blog.csdn.net/weixin_40001704/article/details/81413590