7 Matching Annotations
  1. Nov 2019
  2. Jul 2019
    1. 将错误IP放到数组里面判断是否ping失败三次
      #!/bin/bash  
      IP_LIST="192.168.18.1 192.168.1.1 192.168.18.2"
      for IP in $IP_LIST; do
          NUM=1
          while [ $NUM -le 3 ]; do
              if ping -c 1 $IP > /dev/null; then
                  echo "$IP Ping is successful."
                  break
              else
                  # echo "$IP Ping is failure $NUM"
                  FAIL_COUNT[$NUM]=$IP
                  let NUM++
              fi
          done
          if [ ${#FAIL_COUNT[*]} -eq 3 ];then
              echo "${FAIL_COUNT[1]} Ping is failure!"
              unset FAIL_COUNT[*]
          fi
      done
      
    2. 获取随机8位字符串:
      方法1:
      # echo $RANDOM |md5sum |cut -c 1-8
      471b94f2
      方法2:
      # openssl rand -base64 4
      vg3BEg==
      方法3:
      # cat /proc/sys/kernel/random/uuid |cut -c 1-8
      ed9e032c
      
    3. 获取随机8位数字:

      方法1:

      # echo $RANDOM |cksum |cut -c 1-8
      23648321
      方法2:
      # openssl rand -base64 4 |cksum |cut -c 1-8
      38571131
      方法3:
      # date +%N |cut -c 1-8
      69024815
      
    4. 注意事项

      1)开头加解释器:#!/bin/bash

      2)语法缩进,使用四个空格;多加注释说明。

      3)命名建议规则:变量名大写、局部变量小写,函数名小写,名字体现出实际作用。

      4)默认变量是全局的,在函数中变量local指定为局部变量,避免污染其他作用域。

      5)有两个命令能帮助我调试脚本:set -e 遇到执行非0时退出脚本,set-x 打印执行过程。

      6)写脚本一定先测试再到生产上。

  3. Oct 2017
  4. Feb 2017
    1. A shell script is a file of executable commands that has been stored in a text file. When the file is run, each command is executed.

      The power of BASH!