说明:
CSV 文件:cmd_list1.csv
testcase:对应test case id
function:对应test case的标题
interfacenotes:对应bsp节点名称
cmd:对应bsp命令
needtest:【yes】执行这个case,【no】表示跳过不执行这个case
var1:表示参数1
var2:表示参数2
loop:表示命令或case循环几次,当多条命令需要循环时,命令必须以;拼接。
命令里[]表示参数占位符
1 #!/bin/bash 2 ################################################################# 3 # Copyrith (C) 2020 XXXX Ltd. All rights reserved. # 4 # # 5 # Author :Bing Song # 6 # Create Time : Jan 8,2020 # 7 # Applied Project:XXX # 8 # How to Use:Copy the script into OS in /root,call this script# 9 ################################################################# 10 cmd_list="test.csv" 11 cmd_fail_list="cmd_fail_list.csv" 12 cmd_count=0 13 cmd_skip=0 14 cmd_pass=0 15 cmd_fail=0 16 #分别替换每个字段 17 getTestcase(){ 18 local tc="" 19 tc=`echo $LINE |awk -F "," '{print $1}'` 20 echo $tc 21 } 22 23 getFunction(){ 24 local fn="" 25 fn=`echo $LINE |awk -F "," '{print $2}'` 26 echo $fn 27 } 28 29 getInterface(){ 30 local ifc="" 31 ifc=`echo $LINE |awk -F "," '{print $3}'` 32 echo $ifc 33 } 34 getCmd(){ 35 local cmd="" 36 cmd=`echo $LINE |awk -F "," '{print $4}'` 37 echo $cmd 38 39 } 40 41 getNeedtest(){ 42 local ndt="" 43 ndt=`echo $LINE |awk -F "," '{print $5}'` 44 echo $ndt 45 } 46 47 getVar1(){ 48 local var1="" 49 var1=`echo $LINE |awk -F "," '{print $6}'` 50 echo $var1 51 52 } 53 getVar2(){ 54 local var2="" 55 var2=`echo $LINE|awk -F "," '{print $7}'` 56 echo $var2 57 } 58 59 getLoop(){ 60 local loop="" 61 loop=`echo $LINE|awk -F "," '{print $8}'` 62 echo $loop 63 64 } 65 while read LINE 66 do 67 cmd_tc=$(getTestcase "$LINE") 68 cmd_fun=$(getFunction "$LINE") 69 cmd_ifc=$(getInterface "$LINE") 70 cmd_cli=$(getCmd "$LINE") 71 cmd_ndt=$(getNeedtest "$LINE") 72 cmd_var1=$(getVar1 "$LINE") 73 cmd_var2=$(getVar2 "$LINE") 74 cmd_loop=$(getLoop "$LINE") 75 76 if [ -s $cmd_list ];then 77 if [[ -z "$cmd_tc" ]] || [[ -z "$cmd_cli" ]] || [[ -z "$cmd_ifc" ]];then 78 echo "column ${cmd_tc} ${cmd_cli} ${cmd_ifc} is requited" 79 exit 80 else 81 82 cmd_count=$[$cmd_count+1] 83 if [ "X$cmd_ndt" == X"yes" ];then 84 85 if [[ -n "$cmd_var1" ]] && [[ -n "$cmd_var2" ]];then 86 echo $cmd_cli >> tmp0.log 87 88 #case1:two parameters var1 and var2 89 #setting (1 2) loop method 90 for v2 in $(seq 1 ${cmd_var2[@]}) 91 do 92 echo 93 for v1 in ${cmd_var1[@]} 94 do 95 #loop to insted with $v1 and $v2 96 echo `sed 's/\[\]/'$v1'/1;s/\[\]/'$v2'/' tmp0.log` >> tmp1.log#同时替换2个变量用;分开,【】需要转意 97 done 98 99 done 100 while read line 101 do 102 eval $line 103 echo "#$line" 104 echo $line 105 sleep 1 106 done < tmp1.log 107 rm -rf tmp1.log 108 109 #case2:one parameters var1 110 elif [[ -n "$cmd_var1" ]] && [[ ! -n $cmd_loop ]];then 111 for v1 in ${cmd_var1[@]} 112 do 113 echo "#${cmd_cli/[]/$v1}" 114 cmd_v1=`eval ${cmd_cli/[]/$v1}` 115 echo $cmd_v1 116 sleep 1 117 done 118 #case3:one parameters var2 119 elif [[ -n "$cmd_var2" ]] && [[ ! -n $cmd_loop ]];then 120 for v2 in ${cmd_var2[@]} 121 do 122 echo "#${cmd_cli/[]/$v2}" #用v2值替换[],只替换查找到的第一个匹配的字符串 123 cmd_v2=`eval ${cmd_cli/[]/$v2}` 124 echo $cmd_v2 125 sleep 1 126 done 127 #case4:one parameters var1 and loop 128 elif [[ -n "$cmd_var1" ]]&&[[ -n "$cmd_loop" ]];then 129 for((lp=1;lp<=${cmd_loop};lp++)) 130 do 131 echo 132 for v1 in ${cmd_var1[@]} 133 do 134 echo ${cmd_cli//[]/$v1}|awk -F ';' '{for(i=1;i<=NF;i++) print$i}' >>tmp4.log#v1替换所有【】值,之后,以分号分割,打印所有命令 135 #echo `sed 's/\[\]/'$v1'/g' tmp0.log` >> tmp1.log 136 while read row 137 do 138 echo "#$row" 139 v1_loop=`eval $row` 140 sleep 1 141 echo $v1_loop 142 done < tmp4.log 143 done 144 done 145 rm -rf tmp4.log 146 #case5:no parameters var2 and loop 147 elif [[ -n "$cmd_var2" ]]&&[[ -n "$cmd_loop" ]];then 148 for((lp=1;lp<=${cmd_loop};lp++)) 149 do 150 echo 151 for v2 in ${cmd_var2[@]} 152 do 153 echo ${cmd_cli//[]/$v2}|awk -F ';' '{for(i=1;i<=NF;i++) print$i}' >>tmp5.log 154 while read line 155 do 156 echo "#$line" 157 line=`eval $line` 158 sleep 1 159 echo $line 160 done < tmp5.log 161 162 163 done 164 done 165 rm -rf tmp5.log 166 else 167 echo "#${cmd_cli}" 168 cmd_cli=`eval ${cmd_cli}` 169 echo ${cmd_cli} 170 171 fi 172 173 if [ X"$?" == X"0" ];then 174 cmd_pass=$[$cmd_pass+1] 175 echo "Test cmd: "${cmd_tc}" "${cmd_ifc}" -- [PASS]" 176 else 177 cmd_fail=$[$cmd_fail+1] 178 echo "Test cmd: "${cmd_tc}" "${cmd_ifc}" --[Faild]" 179 cmd_fail_info=${cmd_tc}","${cmd_fun}","${cmd_ifc}","${cmd_cli}","${cmd_ndt} 180 echo $cmd_fail_info |tee -a > $cmd_fail_list 2>&1 181 fi 182 else 183 cmd_skip=$[$cmd_skip+1] 184 fi 185 fi 186 187 else 188 189 echo "file $cmd_list is empty" 190 exit 191 fi 192 #skip first row 193 done < $cmd_list |tail -n+2 #读取文件,从第二行开始读,跳过第一行 194 rm -rf tmp0.log 195 echo "=================================================================" 196 197 echo "Total cmd:" $cmd_count 198 echo "Test pass:" $cmd_pass 199 echo "Test fail:" $cmd_fail 200 echo "Test skip:" $cmd_skip 201 echo "==================================================================" 202 echo "Fail log " 203 cat $cmd_fail_list