• shell学习笔记(二)


    特殊符号:

     

     {} 集合

    ${}变量引用

    #ping ip
    ip=10.10.19.10
    i=1
    while [ $i -le 5 ]
    do
      ping -c1 $ip &>dev/null
      if [ $? -eq 0 ];then
         echo "$ip is up.."
      fi
      let i=i+1
    done
    
    
    # 判断用户输入的是否是数字
    read -p "请输入一个数字:" num 
    if [[ ! "$num"=~^[0-9]+$ ]];then
      echo "你输入的不是数字,程序退出!!!"
      exit
    fi
    
    #判断用户输入的是否是数字
    read -p "请输入一个数字:"num
    
    while true
    do
      if [[ $sum=~^[0-9]+$ ]];then
        break
      else
       read -p "不是数字,请重新输入数值:"num
      fi
    done
    echo “你输入的数字是:$num”
    
    
    #输入后缀名
    read -p "Place input prefix: " prefix
    while true
    do
       if [ -n "$prefix" ];then
          break
        else
          read -p "Place input prefix:" prefix
        fi
    done
    
    for i in `seq $num`
    
    #根据时间创建文件
    touch $(date + %F)_file.txt
    

      

     

     

    back_dir=/var/mysql_back
    
    if ! test -d $back_dir;then
        mkdir -p $back_dir
    fi
    
    if [ ! -d $back_dir ];then
       mkdir -p $back_dir
    fi
    
    if [ $UID -ne 0 ];then  #eq
       echo "没有权限!"
    fi
    
    if [ $USER = "root" ];then
      echo "你没有权限!"
      exit
    fi
    
    cp -rf test_yum.sh /home/alice/
    [-d /home ]; echo $?  
    # 是连接文件
    [ -L /etc/rc.locl ];echo $?
    
    #不是目录就创建
    [ ! -d /ccc ] && mkdir /ccc
    #是一个目录就不创建
    [ -d /ccc ] || mkdir /ccc
    
    
    #判断用户是否存在
    read -p  "Please ipunt a username:" user
    #if id $user &>/dev/null; then 
    id $user &>/dev/null; then
    if [ $? -eq 0 ]; then
       echo "user $user already exists"
      else
         useradd $user
         if [ $? -eq 0 ];then
            echo "$user is created."
         fi   
    fi
    
    #磁盘使用 
    disk_use=`df -Th | grep '/$' | awk '{print $(NF-1)}' | awk -F “%” ‘{print $1}’`
    mail_user=alice
    
    if [ $disk_usr -ge 90 ];then
       echo "`date +%F-H%` disk: ${disk_use}%" |mail -s "disk war..." $mail_user
    fi
    
    #当前内存使用百分比
    mem_used=`free -m |grep '^Mem:' |awk '{print $3}'`
    mem_total=`free -m |grep '^Mem:' |awk '{print $2}'`
    mem_percent=$((mem_used*100/men_total))
    
    war_file=/tmp/mem_war.txt
    rm -rf $war_file
    #if [ $mem_percent>80 ];then
    if (($mem_percent>80));then
       echo "`date +%F-%H` memory:${mem_percent}%" > $war_file
    fi
    
    if [ -f $war_file ];then
       mail -s "mem war..." alias <$war_file
       rm -rf $war_file
    fi
    
    echo "当前内存使用百分比:$mem_percent "
    

      

    #测试字符串长度
    [ -z $var ]; echo $? 
    
    #添加用户
    read -p "Place input number:" num
    if [[ ! "$num" =~ ^[0-9]+$ || "$num" =~ ^0+$ ]];then
       echo "error number!"
       exit
    fi
    
    read -p "Place input prefix: " prefix
    if [ -z "$prefix" ];then
       echo "error prefix"
       exit
    fi
    
    for i in `seq $num`
    do 
       user=$prefix$i
       useradd $user
       echo "123" |passwd --stdin $user &>/dev/null
       if [ $? -eq 0 ];then
          echo "$user is created." 
       fi
    done
    

      

     

     

     删除用户:

    red -p "Place input a username: " user
    
    id $user &>/dev/null
    if [ $? -ne 0 ];then
        echo "no such user: $user"
        exit 1
    fi
    
    read -p "Are you sure?[y/n]: " action
    if [ "$action" !="y" ];then
       echo "good!"
       exit
    fi
    

      不同用户登录:

    trap "" HUP INT OUIT TSTP
    web1=192.168.122.241
    web2=192.168.122.53
    mysql1=192.168.122.210
    clear
    
    while :
    do
        cat <<-EOF
        1.web1
        2.web2
        3.mysql1
        EOF
    
        red -p "input number: " num
        case "$num" in 
        1)
        ssh root@$web1
        ;;
        2) 
        ssh root@$web2
        ;;
        3)
        ssh root@$mysql1
        ;;
    
        "")
        ;;
        *)
        echo "error"
        esac
     done 
    

      简单的系统工具箱:

    menu(){
    cat <<-EOF
      h.help
      f.disk partition
      d.filesystem mount
      m.memory
      u.system load
      q.exit
    EOF
    }
    
    menu
    
    # until false
    while :
    do
    read -p "Please input[h for help]: " action
    case "$action" in
    h)
       clear
       menu
       ;;
    f) 
       fdisk -l
       ;;
    d)
      df -Th
      ;;
    m)
      free -m
      ;;
    u)
      uptime
      ;;
    q)
      #exit
      break
      ;;
    "")
      ;;
    *)
      echo "error"
    esac
    done
    echo "finish..."
    

      

    #查看网关
    ip r 
    
    #删除网关
    ip r del
    

      

    gateway=192.168.122.1
    
    ping -c1 www.baidu.com &>dev/null
    if [ $? -eq 0 ];then
       yum -y install httpd
       systemctl start httpd
       systemctl enable httpd
       firewall-cmd --permanent --add-service=http
       firewall-cmd --permanent --add-service=httpds
       firewall-cmd --reload
       sed -ri '/^SELINUX=/cSELINUX=disabled' /etc/selinux/config
       setenforce 0
        # echo "connet:unreachable"
        # exit
       
       curl http://127.0.0.1 &>/dev/null
       if [ $? -eq 0 ];then
          echo "Apache ok..."
       fi 
    elif ping -c1 $gateway &>/dev/null;then
       echo "check dns..."
    else
       echo "check ip address"
    fi
    
    cat /etc/resolv.conf 
    grep 'nameserver' /etc/resolv.conf
    

      ping 多个 ip

    ip.txt
    for i in {2..254}
    do
        {
        ip =192.168.40.$i
        ping -c1 -W1 $ip &>/dev/null
        if [ $? -eq 0 ];then
            echo "$ip " |tee -a ip.txt
        fi
        }&
    done
    wait
    echo "finishi..."
    

      

    批量创建用户
    #判断脚本是否有参数
    if [ $# -eq 0];then
       echo "usage: `basename $0` file"
       exit 1
    fi
    
    #判断是否是文件
    if [ ! -f $1 ];then
      echo "error file"
      exit 2
    fi
    
    IFS=$'
    '
    for line in `cat $1`
    do 
      if [ ${#line} -eq 0 ];then
        continue
      fi
      user=`echo "$line" |awk '{print $1}'`
      pass=`echo "$line" |awk '{print $2}'`
      id $user &>/dev/null
      if [ $? -eq 0 ];then
         echo "user $user already exists"
      else
         useradd $user
         echo "$pass" |passwd --stdin $user &>/dev/null
        if [ $? -eq 0 ];then
          echo "$user is created."
        fi  
       fi 
    done
    

      

  • 相关阅读:
    C#读取EXCEL中数字无法读取的方法
    学习Wml
    sqlserver.exe进程占cpu100%
    windows phone mango 页面跳转事件顺序
    Windows 8 系列(一):win 8 简介
    windows phone 小应用与随机算法的感悟
    文件下载
    html 表格固定宽度
    excel 导入科学计数法
    word 排版
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/14136048.html
Copyright © 2020-2023  润新知