我正在尝试计算errorList数组中每个项目的错误,但是我一直收到“表达式中的语法错误(错误标记是“:只是一个测试”)”。
#!/bin/bash
declare -a errorList=(
'MEDIA ERROR: just a test'
'MEDIA BLA: resource unavaliable!'
'DIALOG: Found PROBLEM ev_id="EV_ID"'
)
declare -a errorListNames=(
"Silence"
"Unavaliable"
"Environment error"
)
pathLogs="/home/logs_test/"
logFileName="media.log.*"
dateLog="10-10-2019"
fgrep "$dateLog" $pathLogs$logFileName > grepedByDate.txt
for i in "${errorList[@]}"
do
fgrep -c "${ errorList[i], }" grepedByDate.txt
echo "${errorListNames[i]}"
done
echo "Bye"
答案 0 :(得分:1)
!
或#
获取密钥代替:
for i in "${errorList[@]}"
使用:
for i in ${!errorList[@]}"
或:
maxIndex=${#errorList[@]}
for (( i=0; i<$maxIndex; i++ ))
代替:
fgrep -c "${ errorList[i], }" grepedByDate.txt
使用:
fgrep -c "${errorList[i]}" grepedByDate.txt
答案 1 :(得分:1)
您可以使用关联数组:
#!/usr/bin/env bash
declare -A errors=(
[Unavailable]='MEDIA ERROR: Resource unavailable'
)
for i in "${!errors[@]}"; do
echo "$i: ${errors[$i]}"
done
${!errors[@]}
扩展为键(Unavailable
,...)并存储在$i
${errors[$i]}
扩展为给定键$i
(MEDIA ERROR...
,...)的值