shell知识

shell基本的语句

  一:if语句

    格式:

      if [ #条件的判断 ];then

      #执行的动作

      fi

    操作符     

    +  加
    -   减
    *   乘
    /    除【取整数商】
    %  余【取余数】

    数值的比较

参数 说明 示例  
-eq 等于则条件为真 [ 1 -eq 10 ]
-ne 不等于则条件为真 [ 1 -ne 10 ]
-gt 大于则条件为真 [ 1 -gt 10 ]
-lt 小于则条件为真 [ 1 -lt 10 ]
-ge 大于等于则条件为真 [ 1 -ge 10 ]
-le 小于等于则条件为真 [ 1 -le 10 ]

  二:case语句

    格式:

    case #变量   in

      1)

      ;;

      ......

      *)

    esac

    示例:脚本启动关闭重启nginx服务【判断服务是否存活,判断其pid文件,如果没有可以在手动的创建pid文件】   

    #!/usr/bin/bash

    source /etc/init.d/functions

    case $1 in
      start)
        if [ -f /var/run/nginx.pid ];then
          echo "nginx服务已经启动"
          exit
        else
          /usr/sbin/nginx
          action "nginx服务启动" /bin/true
        fi
      ;;
      stop)
        if [ -f /var/run/nginx.pid ];then
          /usr/sbin/nginx -s stop
            if [ $? -eq 0 ];then
              action "nginx服务正在关闭。。。" /bin/false
            else  
              action "nginx服务关闭失败。。。" /bin/false
            fi
          action "nginx服务正在关闭。。。" /bin/false
        fi
      ;;
      reload)
        $0 stop
        $0 start
      ;;
      *)

        echo "USSGE: $0 [ start|stop|reload ]"
    esac

  三:for语句

    示例:根据手动输入用户的前缀与后缀进行创建用户    

    #!/usr/bin/bash
    if [ ! $UID -eq 0 ] && [ ! $USER == “root” ];then
      echo "无权限执行......"
      exit 
    fi

    read -p "请输入你要创建的用户前缀: " user_qz
    if [ -z $user_qz ];then
      echo "请输入有效的值....."
      exit 
    fi


    read -p "请输入你要创建的用户数量: " user_num
      if [[ ! $user_num =~ ^[0-9]+$ ]];then
      echo "请输入整数"
      exit 
    fi


    echo "你创建的用户是 ${user_qz}1 ..${user_qz}${user_num}"
    read -p "你要创建的用户如下,你确定要创建吗?[ y/n ] " readly

    case $readly in
      y|yes|YES)
        for i in $(seq $user_num)
        do
          user=${user_qz}${i}
          id $user &>/dev/null
            if [ $? -eq 0 ];then
              echo "useradd: user $user already exists"
            else

              useradd $user;echo $RANDOM|tee -a backup.txt|passwd --stdin $user &>/dev/null
              echo "useradd: user $user add successfully."

            fi
        done

      ;;
      n|no|NO)

      ;;
      *)
        echo "你想好了再创建......"
      ;;
      esac

      示例二:批量创建用户

      #!/usr/bin/bash

      for i in $(cat user.txt)
      do
      user=$(echo $i|awk -F ":" '{print $1}')
      pass=$(echo $i|awk -F ":" '{print $2}')

      id $user &>/dev/null

      if [ $? -eq 0 ];then
        echo "$user 已存在"
      else
        useradd $user
        echo "$pass" | passwd --stdin $user &>/dev/null
      fi
      done

   四:while语句

     示例:      

    #!/usr/bin/bash
    i=1
    j=10
    while [ $i -lt 10 ]
    do
      sum=$(( $i + $j ))
      echo $i + $j = $sum
      let i--
      let j++
    done

    #!/usr/bin/bash
    while read line
    do

      id $line &>/dev/null
      rc=$?
        if [ $rc -eq 0 ];then
          echo "$line 用户已经创建"
          sleep 1
        else
          useradd $line;echo "123"|passwd --stdin $line &>/dev/null
          echo "$line 用户创建成功"
          sleep 1
        fi

    done<user.txt

   五:函数【函数的传参与位置参数不同】

     #!/usr/bin/bash
     file=/etc/passwd
     t_file(){
      if [ -f $file ];then
        return 20
      else
        return 30
      fi
    }
    t_file

    if [ $? -eq 20 ];then
      echo "文件存在"
    elif [ $? -eq 30 ];then
      echo "文件不存在"
    fi

  六:数组【取值,累加,打印】

    示例:       

    #!/usr/bin/bash
    declare -A info_passwd
    while read line
    do
      type=$(echo $line|awk -F ":" '{print $NF}')
      let info_passwd[$type]++
    done</etc/passwd
    for i in ${!info_passwd[@]}
    do
      echo 索引的名称:$i 索引对应次数: ${info_passwd[$i]}
    done

  七:运算符

    算术:expr 1 + 2     $[ 1 + 2 ] 

    布尔:

     !      非运算,表达式为 true 则返回 false,否则返回 true。

     -o      或运算,有一个表达式为 true 则返回 true。

     -a      与运算,两个表达式都为 true 才返回 true。

    逻辑:

     &&      逻辑的 AND,和的意思

     ||      逻辑的OR,或的意思

    字符串:

      =        检测两个字符串是否相等,相等返回 true        

      !=        检测两个字符串是否相等,不相等返回 true。

       -z        检测字符串长度是否为0,为0返回 true。

      -n        检测字符串长度是否为0,不为0返回 true

      $a        检测字符串是否为空,不为空返回 true

原文地址:https://www.cnblogs.com/yexiuer/p/10745362.html