0

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.

Shmuel
  • 341
cptully
  • 119
  • 4
  • 3
    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:44
  • 2
    I suspect your problem is that you're not using array assignment syntax in ERRORSTORES= -- you need a surrounding pair of parenthesis. Untested, otherwise. – Jeff Schaller Sep 26 '19 at 15:44
  • EERORSTORES is supposed to be an array of IP addresses, grepped from our log files. @Jeff Schaller, if I understand your comment, then ERRORSTORES=$(...) should be ERRORSTORES=($(...)). Is that what you meant? – cptully Sep 26 '19 at 17:28
  • Jeff's comment lead me to: https://stackoverflow.com/questions/9449417/how-do-i-assign-the-output-of-a-command-into-an-array – cptully Sep 26 '19 at 17:42
  • Write the whole thing in awk. or perl. or python. or anything other than a shell loop that forks grep and awk and whatever else multiple times per iteration. See Why is using a shell loop to process text considered bad practice?. BTW, perl would be a good choice because it has the File::Find module, which offers similar functionality to the find command. – cas Sep 27 '19 at 05:14
  • I have gotten the script working thanks to earlier comments. @cas I will look into your suggestions next time I dive into that script. Thank you for the suggestion. – cptully Oct 08 '19 at 14:58

0 Answers0