0

I am running a bash script that looks through error logs and produces a report that prints in a Mac Terminal.

Currently, the output looks like this:

This error report is derived from Command Logs from ________ to __________.

IP addresses:

109.xx.xxx.xxx

Error Codes List: 

(code 12)
(code 30)

Common Errors - Count: 

Code 2: 
Code 10: 
Code 11: 
Code 12: 237
Code 14: 
Code 20: 
Code 23: 
Code 30: 5
Code 35: 
Code 37: 
Code 52: 
/usr/local/bin/backerr: line 45: `$skipping': not a valid identifier
Total: 242

Total Files Transferred: 
3558

Count complete.

The following are details for each error:   
// all of the 242 above-referenced lines //

Skipped files: 
// lists all of the lines that have the word 'skipping\ ' //

As you can see, I do not have the correct identifier:

    /usr/local/bin/backerr: line 45: `$skipping': not a valid identifier

This is the code I have to detect the lines with 'skipping\ ' in them, count the total, and print the total under the Code XX: total above. Also, I want the 'skipping\ ' total to add to 'Total: 242'.

  count=()
  total=0

  while read skipping; do
        (( count[$skipping]++, total++ ))      
  done < <(grep 'skipping\ ' $input_variable)

  for $skipping in $input_variable; do
    echo "Skipped: ${count[$skipping]}"
  done

My expected output of the "Common Errors - Count:" section should be as follows:

    Common Errors - Count: 

    Code 2: 
    Code 10: 
    Code 11: 
    Code 12: 237
    Code 14: 
    Code 20: 
    Code 23: 
    Code 30: 5
    Code 35: 
    Code 37: 
    Code 52: 
    Skipped: 107 (I made up this number for this question)
    Total: 349

I am stuck here. Please help.

As a work-around, instead of using the code below:

      while read skipping; do
            (( count[$skipping]++, total++ ))      
      done < <(grep 'skipping\ ' $input_variable)

      for $skipping in $input_variable; do
        echo "Skipped: ${count[$skipping]}"
      done

I replaced that with:

  echo "Total Skipped Files: " ; egrep -c 'skipping' $input_variable ;

But, this does not really produce the ideal outcome as I originally envisioned.

6
  • why < < and why not < Commented Jan 6, 2016 at 18:39
  • @SMA: Process substitution: treats the output of a command like a file. Commented Jan 6, 2016 at 18:45
  • Underneath all of the Code totals (Code 2: Code 10: Code 11: Code 12: 237 Code 14: Code 20: Code 23: Code 30: 5 Code 35: Code 37: Code 52:)' I would like to have the a row that says `Skipped: 'n'' where is 'n' is the total. Then, I would like to add the Skipped total to the Total at the bottom. Do you understand? Commented Jan 6, 2016 at 20:23
  • I just updated the question with the desired output. Thank you. Commented Jan 6, 2016 at 20:58
  • Hmm but how are you getting 107 and 349 numbers from above input? Commented Jan 6, 2016 at 21:10

1 Answer 1

2

Your for loop shouldn't have a dollar sign for the indexing variable:

for skipping in $input_variable; do
   ^^^ 
Sign up to request clarification or add additional context in comments.

3 Comments

I did that, but I get `/usr/local/bin/script: line 44: filename: syntax error: invalid arithmetic operator (error token is "@filename"'
@technerdius Where is "filename" in your script?
filename is $input_variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.