• getopts以参数形式执行diag


      1 #!/bin/bash
      2 #################################################################################
      3 # Copyright(C) 2019 xxx Ltd. All rights reserved.                               #
      4 # File Name        :cel-led-test                                                #
      5 # Author           :Bing Song                                                   #
      6 # Created Time     :12/2/2019                                                   #
      7 # Applied Porject  :xx                                                          #
      8 # How to Use       :Copy this script into "root/" directory and run it directly.#
      9 #################################################################################
     10 
     11 
     12 
     13 #Defining variables
     14 LED_PATH="/sys/bus/i2c/devices/15-0060"
     15 FAN_TRARY_PATH="/sys/devices/platform/soc/fd880000.i2c-pld/i2c-0/i2c-4/i2c-5/i2c-10/10-0066"
     16 DEVICE_ID=
     17 COLOR=
     18 TYPE=
     19 function usage() #定义帮助内容样式
     20 {
     21     echo -e "Usage::
    "
     22     echo -e "	-a                     test all config"
     23     echo -e "	-r                     read operation"
     24     echo -e "	-w                     write operation"
     25         echo -e "	-d                     device id.Example with psu,-d 1 or 2"
     26         echo -e "	-t                     [system|fan|psu|sfp]"
     27         echo -e "	-c                     [red|green|off|bule|white]"
     28         echo -e "	-h                     display this help context and exit"
     29 echo -e "Example: 
    "
     30 echo -e " -a                            #test all config"
     31 echo -e " -r -t system -d 1             #get system led status"
     32 echo -e " -r -t psu -d 1                #get psu1 led status"
     33 echo -e " -w -t fan -d 1 -c red         #set fan1 led to red"
     34 }
     35 function led_read(){
     36     
     37 
     38 
     39     if [ "$TYPE" = "system" ];then#判断LED类型为system led
     40
     41         cat "$LED_PATH"/system_led > tmp.log
     42         data=`cat tmp.log`
     43         printf "Reading ($TYPE) LED,value:("$data") -- :Passed 
    "
     44         rm -rf tmp.log
     45     elif [ "$TYPE" = "psu" ];then
     46         
     47         cat "$LED_PATH"/pwr"$DEVICE_ID"_led > tmp.log
     48        data=`cat tmp.log`
     49        printf "Reading ($TYPE$DEVICE_ID) LED,value:("$data") -- :Passed 
    "
     50        rm -rf tmp.log
     51     elif [ "$TYPE" = "fan" ];then
     52         if [ "$DEVICE_ID" = "0" ];then
     53           
     54             cat "$LED_PATH"/fan_led > tmp.log
     55             data=`cat tmp.log`
     56             printf "Reading ($TYPE) LED,value:("$data") -- :Passed 
    "
     57             rm -rf tmp.log
     58         else
     59             #echo "#cat $FAN_TRARY_PATH/fan"$DEVICE_ID"_led"
     60             cat "$FAN_TRARY_PATH"/fan"$DEVICE_ID"_led > tmp.log 
     61             data=`cat tmp.log`
     62             printf "Reading ($TYPE$DEVICE_ID) LED,value:("$data") -- :Passed 
    "
     63             rm -rf tmp.log
     64 
     65         fi
     66     else
     67        check_type
     68 
     69     fi
     70 }
     71 
     72 function led_write(){
     73     #echo "function kcs:-$OPTARG and $OPTIND"
     74  if [ "$TYPE" = "system" ];then
     75         #echo "#echo $COLOR > $LED_PATH/system_led"
     76         echo $COLOR > "$LED_PATH"/system_led 
     77         if [ $? -eq 0 ];then
     78             printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Passed 
    "
     79         else
     80              printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed 
    "
     81         fi
     82     elif [ "$TYPE" = "psu" ];then
     83        # echo "#echo $COLOR > $LED_PATH/pwr"$DEVICE_ID"_led"
     84         echo $COLOR > "$LED_PATH"/pwr"$DEVICE_ID"_led 
     85         if [ $? -eq 0 ];then
     86             printf "Wrinting ($TYPE$DEVICE_ID) LED,value:("$COLOR") -- :Passed 
    "
     87         else
     88             printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed 
    "
     89         fi
     90     elif [ "$TYPE" = "fan" ];then
     91         if [ "$DEVICE_ID" = "0" ];then
     92             #echo "#echo $COLOR > $LED_PATH/fan_led"
     93             echo "$COLOR" > "$LED_PATH"/fan_led 
     94             if [ $? -eq 0 ];then
     95                 printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Passed 
    "
     96             else
     97                  printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed 
    "
     98             fi
     99         else
    100             #echo "#echo $COLOR >  $FAN_TRARY_PATH/fan"$DEVICE_ID"_led"
    101             echo $COLOR > "$FAN_TRARY_PATH"/fan"$DEVICE_ID"_led
    102             if [ $? -eq 0 ];then
    103                 printf "Wrinting ($TYPE$DEVICE_ID) LED,value:("$COLOR") -- :Passed 
    "
    104             else
    105                 printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed 
    "
    106             fi
    107         fi
    108     else
    109        check_type
    110 
    111     fi
    112 
    113 }
    114 function check_type(){ #定义函数,校验led type
    115    if [ "$TYPE" = "psu" ];then
    116        return 0
    117    elif [ "$TYPE" = "fan" ];then
    118        return 0
    119    elif [ "$TYPE" = "system" ];then
    120        return 0
    121    else
    122       return 1
    123    fi
    124 
    125 }
    126 function check_color_mode(){ #定义函数,校验led color 种类
    127    if [ "$COLOR" = "red" ];then
    128        return 0
    129    elif [ "$COLOR" = "green" ];then
    130        return 0
    131    elif [ "$COLOR" = "off" ];then
    132        return 0
    133    else
    134       return 1
    135    fi
    136   
    137 }
    138 
    139 function check_device_id(){ #定义函数,校验device id
    140 local psu_dev_arry=(1 2)
    141 local fan_dev_arry=(0 1 2 3 4 5 6 7)
    142 if [ "$TYPE" = "psu" ];then
    143    for i in ${psu_dev_arry[@]}
    144    do
    145       if [ "$DEVICE_ID" = $i ];then
    146          return 0
    147       fi
    148    done
    149    return 1
    150 elif [ "$TYPE" = "fan" ];then
    151     for i in ${fan_dev_arry[@]}
    152     do
    153       if [ "$DEVICE_ID" = $i ];then
    154          return 0
    155       fi
    156    done
    157    return 1
    158 fi
    159 }
    160 
    161 function all(){ #定义函数all()去执行带--all参数的选项
    162 #system
    163     local color_arry=(red green off)
    164     local test_type=(system psu fan fan-tray)
    165     local pid=1
    166     local psu_id=(1 2)
    167     local fan_id=(1 2 3 4 5 6 7 8 9 10 11 12 13 14)
    168     local ERROR_COUNT=0
    169     printf "==================================LED TEST ALL===========================================
    "#printf进行打印格式化
    170     printf "Type:${test_type[0]}
    "
    171     echo   "-----------------------------------------------------------------------------------------"
    172     printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|
    " ID Name Value Result Value Restult Value Result #printf打印格式化,%10表示字段间距
    表回车,s表示占位符,代替后面的实际字段
    173     echo   "-----------------------------------------------------------------------------------------"
    174     printf "|%-10s|%10s|" 1 System
    175         for i in ${color_arry[@]}
    176         do
    177             echo $i > "$LED_PATH"/system_led
    178             sleep 1
    179             if [ $? -eq 0 ];then #判断上条命令是否执行成功
    180                 printf "%10s|33[32m%10s33[0m|" $i Passed #Passed字体绿色显示32m
    181             
    182             else
    183                  printf "%10s|33[31m%10s33[0m|" $i Failed #Failed 字体红色显示31m
    184                  ERROR_COUNT=$[$ERROR_COUNT+1]#TBD
    185                  exit 1
    186             fi
    187        done
    188     printf "
    "
    189     echo   "-----------------------------------------------------------------------------------------"
    190     printf "Type:${test_type[1]}
    "
    191     echo   "-----------------------------------------------------------------------------------------"
    192     printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|
    " ID Name Value Result Value Restult Value Result
    193     echo   "-----------------------------------------------------------------------------------------"
    194 #PSU
    195 
    196     printf "|%-10s|%10s|" $pid ${test_type[1]}
    197     for j in ${psu_id[*]}
    198     do
    199         for i in ${color_arry[*]}
    200         do
    201         
    202             echo $i > "$LED_PATH"/pwr"$j"_led
    203             sleep 1
    204             if [ $? -eq 0 ];then
    205                 printf "%10s|33[32m%10s33[0m|" $i Passed
    206 
    207             else
    208                  printf "%10s|33[31m%10s33[0m|" $i Failed
    209                  ERROR_COUNT=$[$ERROR_COUNT+1]
    210 
    211                  exit 1
    212             fi
    213 
    214         done
    215      printf "
    "
    216      j=$[$j+1]
    217      if [ "$j" = "2" ];then
    218         printf "|%-10s|%10s|" $j ${test_type[1]$j}
    219      else
    220         continue
    221      fi
    222      done
    223        printf "
    "
    224     echo   "-----------------------------------------------------------------------------------------"
    225 #front panle fan
    226     printf "Type:${test_type[2]}
    "
    227     echo   "-----------------------------------------------------------------------------------------"
    228     printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|
    " ID Name Value Result Value Restult Value Result
    229     echo   "-----------------------------------------------------------------------------------------"
    230     printf "|%-10s|%10s|" $pid ${test_type[2]}
    231         for i in ${color_arry[@]}
    232         do
    233             echo $i > "$LED_PATH"/fan_led
    234             sleep 1
    235             if [ $? -eq 0 ];then
    236                 printf "%10s|33[32m%10s33[0m|" $i Passed
    237 
    238             else
    239                  printf "%10s|33[31m%10s33[0m|" $i Failed
    240                  ERROR_COUNT=$[$ERROR_COUNT+1]
    241                                   
    242                  exit 1
    243             fi
    244 
    245        done
    246        printf "
    "
    247     echo   "-----------------------------------------------------------------------------------------"
    248 #fan-tray
    249     printf "Type:${test_type[3]}
    "
    250     echo   "-----------------------------------------------------------------------------------------"
    251     printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|
    " ID Name Value Result Value Restult Value Result
    252     echo   "-----------------------------------------------------------------------------------------"
    253     printf "|%-10s|%10s|" $pid ${test_type[3]}
    254     
    255     for j in ${fan_id[*]}
    256     do
    257             for i in ${color_arry[*]}
    258             do
    259                 echo $i > "$FAN_TRARY_PATH"/fan"$j"_led
    260                 sleep 1
    261                 if [ $? -eq 0 ];then
    262                 printf "%10s|33[32m%10s33[0m|" $i Passed
    263 
    264                 else
    265                  printf "%10s|33[31m%10s33[0m|" $i Failed
    266                  ERROR_COUNT=$[$ERROR_COUNT+1]
    267 
    268                     exit 2
    269                 fi
    270             done
    271      printf "
    "
    272     j=$[$j+1]
    273    if [ "$j" = "14" ];then
    274           break
    275          
    276       else
    277           
    278            printf "|%-10s|%10s|" $j ${test_type[3]}
    279           continue
    280       fi
    281     done
    282         printf "
    "
    283     echo   "-----------------------------------------------------------------------------------------"
    284    if [ "$ERROR_COUNT" -eq 0 ];then
    285        echo -e "All LED test results :--[ 33[32m Passed 33[0m ]"
    286    else
    287        echo "All LED test results :--[ 33[31m Failed 33[0m ]"
    288        echo $ERROR_COUNT
    289    fi
    290 }
    291 
    292 
    293 
    294 if [ $# -lt 1 ];then #判断是否输入至少一个参数
    295     echo "argument is os few"
    296     usage
    297     exit
    298 fi
    299 while getopts ":arwd:t:c:h" opt #命令选项结构,:表示选项后面带参数
    300 do
    301   case $opt in 
    302 a|all )
    303     test_mode=all                
    304         ;;
    305 r|read )
    306     test_mode=read
    307         ;;
    308 w|write )
    309         test_mode=write
    310         ;;
    311 d|device )
    312         DEVICE_ID=$OPTARG
    313         ;;
    314 t|type )
    315         TYPE=$OPTARG
    316         ;;
    317 c|color ) 
    318         COLOR=$OPTARG
    319         ;;
    320 h|help )
    321         usage
    322         exit 0  
    323         ;;
    324 ?)
    325       echo "invalid argument:-$OPTARG"
    326       usage
    327       exit 1
    328       ;;
    329 
    330    esac
    331 done
    332 
    333 if [ "$test_mode" = "all" ];then
    334       all
    335 elif [ "$test_mode" = "read" ];then
    336 
    337    check_type
    338    if [ $? -eq 1 ];then
    339        echo "type $TYPE not be found"
    340        exit 1
    341    fi
    342   check_device_id
    343    if [ $? -eq 1 ];then
    344        echo "device id[:-d] $DEVICE_ID not be found"
    345        exit 1
    346    fi
    347     led_read
    348 elif [ "$test_mode" = "write" ];then
    349     
    350    check_type
    351    if [ $? -eq 1 ];then
    352        echo "type $TYPE not be found"
    353        exit 1
    354    fi
    355   check_device_id
    356    if [ $? -eq 1 ];then
    357        echo "device id $DEVICE_ID not be found"
    358        exit 1
    359    fi
    360    check_color_mode
    361    if [ $? -eq 1 ];then
    362        echo "not support $COLOR mode"
    363        exit 1
    364    fi
    365 
    366    led_write
    367 else
    368      echo "unknow option"
    369 fi
  • 相关阅读:
    js实现全选反选功能
    seajs笔记
    延迟加载图片
    JavaScript 数组基本操作
    图片上传,头像上传
    命令模式
    动态加载js css 插件
    发布-订阅模式
    js 切换全屏
    JavaScript 经典之一 闭包
  • 原文地址:https://www.cnblogs.com/maisha/p/12185987.html
Copyright © 2020-2023  润新知