#!/bin/bash
#################################################################################
# Copyright(C) 2019 xxx Ltd. All rights reserved. #
# File Name :cel-led-test #
# Author :Bing Song #
# Created Time :12/2/2019 #
# Applied Porject :xx #
# How to Use :Copy this script into "root/" directory and run it directly.#
################################################################################# #Defining variables
LED_PATH="/sys/bus/i2c/devices/15-0060"
FAN_TRARY_PATH="/sys/devices/platform/soc/fd880000.i2c-pld/i2c-0/i2c-4/i2c-5/i2c-10/10-0066"
DEVICE_ID=
COLOR=
TYPE=
function usage() #定义帮助内容样式
{
echo -e "Usage::\n"
echo -e "\t-a test all config"
echo -e "\t-r read operation"
echo -e "\t-w write operation"
echo -e "\t-d device id.Example with psu,-d 1 or 2"
echo -e "\t-t [system|fan|psu|sfp]"
echo -e "\t-c [red|green|off|bule|white]"
echo -e "\t-h display this help context and exit"
echo -e "Example: \n"
echo -e " -a #test all config"
echo -e " -r -t system -d 1 #get system led status"
echo -e " -r -t psu -d 1 #get psu1 led status"
echo -e " -w -t fan -d 1 -c red #set fan1 led to red"
}
function led_read(){ if [ "$TYPE" = "system" ];then#判断LED类型为system led cat "$LED_PATH"/system_led > tmp.log
data=`cat tmp.log`
printf "Reading ($TYPE) LED,value:("$data") -- :Passed \n"
rm -rf tmp.log
elif [ "$TYPE" = "psu" ];then cat "$LED_PATH"/pwr"$DEVICE_ID"_led > tmp.log
data=`cat tmp.log`
printf "Reading ($TYPE$DEVICE_ID) LED,value:("$data") -- :Passed \n"
rm -rf tmp.log
elif [ "$TYPE" = "fan" ];then
if [ "$DEVICE_ID" = "" ];then cat "$LED_PATH"/fan_led > tmp.log
data=`cat tmp.log`
printf "Reading ($TYPE) LED,value:("$data") -- :Passed \n"
rm -rf tmp.log
else
#echo "#cat $FAN_TRARY_PATH/fan"$DEVICE_ID"_led"
cat "$FAN_TRARY_PATH"/fan"$DEVICE_ID"_led > tmp.log
data=`cat tmp.log`
printf "Reading ($TYPE$DEVICE_ID) LED,value:("$data") -- :Passed \n"
rm -rf tmp.log fi
else
check_type fi
} function led_write(){
#echo "function kcs:-$OPTARG and $OPTIND"
if [ "$TYPE" = "system" ];then
#echo "#echo $COLOR > $LED_PATH/system_led"
echo $COLOR > "$LED_PATH"/system_led
if [ $? -eq 0 ];then
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Passed \n"
else
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed \n"
fi
elif [ "$TYPE" = "psu" ];then
# echo "#echo $COLOR > $LED_PATH/pwr"$DEVICE_ID"_led"
echo $COLOR > "$LED_PATH"/pwr"$DEVICE_ID"_led
if [ $? -eq 0 ];then
printf "Wrinting ($TYPE$DEVICE_ID) LED,value:("$COLOR") -- :Passed \n"
else
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed \n"
fi
elif [ "$TYPE" = "fan" ];then
if [ "$DEVICE_ID" = "" ];then
#echo "#echo $COLOR > $LED_PATH/fan_led"
echo "$COLOR" > "$LED_PATH"/fan_led
if [ $? -eq 0 ];then
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Passed \n"
else
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed \n"
fi
else
#echo "#echo $COLOR > $FAN_TRARY_PATH/fan"$DEVICE_ID"_led"
echo $COLOR > "$FAN_TRARY_PATH"/fan"$DEVICE_ID"_led
if [ $? -eq 0 ];then
printf "Wrinting ($TYPE$DEVICE_ID) LED,value:("$COLOR") -- :Passed \n"
else
printf "Wrinting ($TYPE) LED,value:("$COLOR") -- :Failed \n"
fi
fi
else
check_type fi }
function check_type(){ #定义函数,校验led type
if [ "$TYPE" = "psu" ];then
return 0
elif [ "$TYPE" = "fan" ];then
return 0
elif [ "$TYPE" = "system" ];then
return 0
else
return 1
fi }
function check_color_mode(){ #定义函数,校验led color 种类
if [ "$COLOR" = "red" ];then
return 0
elif [ "$COLOR" = "green" ];then
return 0
elif [ "$COLOR" = "off" ];then
return 0
else
return 1
fi } function check_device_id(){ #定义函数,校验device id
local psu_dev_arry=(1 2)
local fan_dev_arry=(0 1 2 3 4 5 6 7)
if [ "$TYPE" = "psu" ];then
for i in ${psu_dev_arry[@]}
do
if [ "$DEVICE_ID" = $i ];then
return 0
fi
done
return 1
elif [ "$TYPE" = "fan" ];then
for i in ${fan_dev_arry[@]}
do
if [ "$DEVICE_ID" = $i ];then
return 0
fi
done
return 1
fi
} function all(){ #定义函数all()去执行带--all参数的选项
#system
local color_arry=(red green off)
local test_type=(system psu fan fan-tray)
local pid=1
local psu_id=(1 2)
local fan_id=(1 2 3 4 5 6 7 8 9 10 11 12 13 14)
local ERROR_COUNT=0
printf "==================================LED TEST ALL===========================================\n"#printf进行打印格式化
printf "Type:${test_type[0]}\n"
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|\n" ID Name Value Result Value Restult Value Result #printf打印格式化,%10表示字段间距\n表回车,s表示占位符,代替后面的实际字段
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|" 1 System
for i in ${color_arry[@]}
do
echo $i > "$LED_PATH"/system_led
sleep 1
if [ $? -eq 0 ];then #判断上条命令是否执行成功
printf "%10s|\033[32m%10s\033[0m|" $i Passed #Passed字体绿色显示32m else
printf "%10s|\033[31m%10s\033[0m|" $i Failed #Failed 字体红色显示31m
ERROR_COUNT=$[$ERROR_COUNT+1]#TBD
exit 1
fi
done
printf "\n"
echo "-----------------------------------------------------------------------------------------"
printf "Type:${test_type[1]}\n"
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|\n" ID Name Value Result Value Restult Value Result
echo "-----------------------------------------------------------------------------------------"
#PSU printf "|%-10s|%10s|" $pid ${test_type[1]}
for j in ${psu_id[*]}
do
for i in ${color_arry[*]}
do echo $i > "$LED_PATH"/pwr"$j"_led
sleep 1
if [ $? -eq 0 ];then
printf "%10s|\033[32m%10s\033[0m|" $i Passed else
printf "%10s|\033[31m%10s\033[0m|" $i Failed
ERROR_COUNT=$[$ERROR_COUNT+1] exit 1
fi done
printf "\n"
j=$[$j+1]
if [ "$j" = "" ];then
printf "|%-10s|%10s|" $j ${test_type[1]$j}
else
continue
fi
done
printf "\n"
echo "-----------------------------------------------------------------------------------------"
#front panle fan
printf "Type:${test_type[2]}\n"
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|\n" ID Name Value Result Value Restult Value Result
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|" $pid ${test_type[2]}
for i in ${color_arry[@]}
do
echo $i > "$LED_PATH"/fan_led
sleep 1
if [ $? -eq 0 ];then
printf "%10s|\033[32m%10s\033[0m|" $i Passed else
printf "%10s|\033[31m%10s\033[0m|" $i Failed
ERROR_COUNT=$[$ERROR_COUNT+1] exit 1
fi done
printf "\n"
echo "-----------------------------------------------------------------------------------------"
#fan-tray
printf "Type:${test_type[3]}\n"
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|%10s|%10s|%10s|%10s|%10s|%10s|\n" ID Name Value Result Value Restult Value Result
echo "-----------------------------------------------------------------------------------------"
printf "|%-10s|%10s|" $pid ${test_type[3]} for j in ${fan_id[*]}
do
for i in ${color_arry[*]}
do
echo $i > "$FAN_TRARY_PATH"/fan"$j"_led
sleep 1
if [ $? -eq 0 ];then
printf "%10s|\033[32m%10s\033[0m|" $i Passed else
printf "%10s|\033[31m%10s\033[0m|" $i Failed
ERROR_COUNT=$[$ERROR_COUNT+1] exit 2
fi
done
printf "\n"
j=$[$j+1]
if [ "$j" = "" ];then
break else printf "|%-10s|%10s|" $j ${test_type[3]}
continue
fi
done
printf "\n"
echo "-----------------------------------------------------------------------------------------"
if [ "$ERROR_COUNT" -eq 0 ];then
echo -e "All LED test results :--[ \033[32m Passed \033[0m ]"
else
echo "All LED test results :--[ \033[31m Failed \033[0m ]"
echo $ERROR_COUNT
fi
} if [ $# -lt 1 ];then #判断是否输入至少一个参数
echo "argument is os few"
usage
exit
fi
while getopts ":arwd:t:c:h" opt #命令选项结构,:表示选项后面带参数
do
case $opt in
a|all )
test_mode=all
;;
r|read )
test_mode=read
;;
w|write )
test_mode=write
;;
d|device )
DEVICE_ID=$OPTARG
;;
t|type )
TYPE=$OPTARG
;;
c|color )
COLOR=$OPTARG
;;
h|help )
usage
exit 0
;;
?)
echo "invalid argument:-$OPTARG"
usage
exit 1
;; esac
done if [ "$test_mode" = "all" ];then
all
elif [ "$test_mode" = "read" ];then check_type
if [ $? -eq 1 ];then
echo "type $TYPE not be found"
exit 1
fi
check_device_id
if [ $? -eq 1 ];then
echo "device id[:-d] $DEVICE_ID not be found"
exit 1
fi
led_read
elif [ "$test_mode" = "write" ];then check_type
if [ $? -eq 1 ];then
echo "type $TYPE not be found"
exit 1
fi
check_device_id
if [ $? -eq 1 ];then
echo "device id $DEVICE_ID not be found"
exit 1
fi
check_color_mode
if [ $? -eq 1 ];then
echo "not support $COLOR mode"
exit 1
fi led_write
else
echo "unknow option"
fi

getopts以参数形式执行diag的更多相关文章

  1. linux命令行传递参数定期执行PHP文件

    最近在做一个项目,需要在linux下传递参数定期执行PHP文件,网上查询资料,确实有相关资料,现整理如下: 1.linux执行PHP文件 #{PHP安装bin路径} {PHP文件路径} {需要参数1 ...

  2. bytectf2019 boring_code的知识学习&&无参数函数执行&&上海市大学生CTF_boring_code+

    参赛感悟 第三次还是第二次参加这种CTF大赛了,感悟和学习也是蛮多的,越发感觉跟大佬的差距明显,但是还是要努力啊,都大三了,也希望出点成绩.比赛中一道WEB都没做出来,唯一有点思路的只有EZCMS,通 ...

  3. Struts2之Action三种接收参数形式与简单的表单验证

    有了前几篇的基础,相信大家对于Struts2已经有了一个很不错的认识,本篇我将为大家介绍一些关于Action接收参数的三种形式,以及简单的表单验证实现,下面进入正题,首先我们一起先来了解一下最基本的A ...

  4. requests接口自动化5-表单参数形式的post请求:data

    post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded(表单形式) a ...

  5. java 方法参数的执行顺序

    java方法的参数的执行顺序是从左到右还是从右到左呢? 写出一下测试程序: 1 import java.util.*; 2 import java.io.*; 3 public class Test ...

  6. ABAP 通过sumbit调用另外一个程序使用job形式执行-简单例子

    涉及到两个程序: ZTEST_ZUMA02 (主程序) ZTEST_ZUMA(被调用的程序,需要以后台job执行) "ztest_zuma 的代码 DATA col TYPE i VALUE ...

  7. postgresql shell脚本传递参数并执行sql脚本并

    参考: https://stackoverflow.com/questions/7389416/postgresql-how-to-pass-parameters-from-command-line ...

  8. Android之AlarmManager(全局定时器/闹钟)指定时长或以周期形式执行某项操作

    1.AlarmManager,顾名思义,就是“提醒”,是Android中常用的一种系统级别的提示服务,可以实现从指定时间开始,以一个固定的间隔时间执行某项操作,所以常常与广播(Broadcast)连用 ...

  9. lr_save_var() 截取任意字符串长度,以参数形式输出(参数转变量)

    Action() { char * desc=(char *)malloc(10);  //定义指针变量,以变量形式存放截取到的参数值 // char * aa = "jadkshfkasd ...

随机推荐

  1. Python socket day5

    下载文件 程序04,05 服务端在接收到文件名时应该用try来打开文件,不应该用with open来打开否则,如果文件名不存在,用with open 会出错误 客户端要判断服务端发送的数据是否为空,不 ...

  2. hadoop学习笔记(六):hadoop全分布式集群的环境搭建

    本文原创,如需转载,请注明作者以及原文链接! 一.前期准备: 1.jdk安装        不要用centos7自带的openJDK2.hostname    配置       配置位置:/etc/s ...

  3. Java环境配置与编译运行详解

    这篇文章主要为大家详细介绍了Java环境配置与编译运行的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 一.开篇 通过对之前Java之路的了解之后,相信初学者们都对Java有了一个比较深印 ...

  4. Dart语言学习(五)Dart Bool类型

    Dart Bool类型和其他语言类似,比较简单 其特点有: 1.使用 bool 表示布尔类型 2.布尔值只有 true 和 false 3.布尔类型bool默认值是null bool isTrue = ...

  5. 获取度量数据:创建服务账户获取访问token

    kubectl create clusterrolebinding kubelet-api-test --clusterrole=system:kubelet-api-admin --servicea ...

  6. CSS 动画过程及间接实现样式延时

    /* 过度动画自动归位 */ @keyframes animation_button_scene { 0% { background: #9cacb4; } 10% { /* 样式过度2 */ } 6 ...

  7. github是什么,有什么用

    转载连接:https://blog.csdn.net/obkoro1/article/details/68066441 写在前面:关于github的文章我已经写了两篇了,关于github个人网站搭建和 ...

  8. mcast_get_loop函数

    #include <errno.h> #include <net/if.h> #include <sys/socket.h> #include <netine ...

  9. 使用axios对安卓或者ios低版本兼容性处理

    原因:不支持ES6,无法使用promise 解决办法: 1.安装 es6-promise cnpm install es6-promise --save-dev 2.引入 es6-promise im ...

  10. @media screen 自适应笔记

    在css中使用@media screen 通过检索宽度 对应改变html中class的css属性. 1280分辨率以上(大于1200px) @media screen and (min-width:1 ...