Yes similar questions have been asked, but none of those answers have shown me what I am missing here.
I think my problem is in not fully understanding what I am generating with my code...
LOGFILE=$1
ERROR=$2
START_TIME=$3
END_TIME=$4
FILEDATE=$(echo "${NOW//-}")
FINDSTR='10\.22\.*'
GREP=/bin/zgrep
for (( HOUR=START_TIME ; HOUR <= END_TIME ; HOUR++ )); do
DATE=$(printf "$NOW %02d" $HOUR )
echo
printf "%02d:00\n" $HOUR
ERRORSTORES=$($GREP "$ERROR" $( find /opt/logs/ -name $LOGFILE-$FILEDATE* |
grep $FINDSTR ) | grep "$DATE" | awk -F ' ' '{ print $4 }' |
awk '{a[$0]++}END{for(i in a){printf ("%s\t%d\n", i, a[i])}}')
printf "Stores showing \"%s\": %d\n" "$ERROR" "${#ERRORSTORES[*]}"
printf '%s\n' "${ERRORSTORES[@]}"
done
I have tried to simplify the code for purposes of this post. I have replaced a large chunk of working code that gets command line input with simple VAR=$n assignments
My problem is with the line
`printf "Stores showing \"%s\": %d\n" "$ERROR" "${#ERRORSTORES[@]}"`
From everything I have been able to find her and on other sites, the "${#array[@]}"
should give me a count of the elements in the array. But "${#ERRORSTORES[@]}"
is always printing 1 or 0!
I suspect now that I am composing this question that my problem lies in the printf of my awk filter at the end of the ERRORSTORES=$(...)
line. I am guessing that my printf ending with \n is giving me the output that I want (each element on a newline) but confusing the "${#array[@]}"
pattern.
As I understand things, I need a null character instead of \n
but so far I am not coming up with a way to replace \n with a null character. I have tried \0
and \n\0
. The first strings my output together on one big line. The second still gives me the one element per line that I want but also gives a count of 1.
ERRORSTORES
is not an array. You assign to it with a command substitution. This will give it a single value. It's unclear what you want this variable to contain. Also,bash
variables (arrays or ordinary ones) can't contain\0
. – Kusalananda Sep 26 '19 at 15:44ERRORSTORES=
-- you need a surrounding pair of parenthesis. Untested, otherwise. – Jeff Schaller Sep 26 '19 at 15:44ERRORSTORES=$(...)
should beERRORSTORES=($(...))
. Is that what you meant? – cptully Sep 26 '19 at 17:28File::Find
module, which offers similar functionality to thefind
command. – cas Sep 27 '19 at 05:14