LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Reading telemetry data and applying an action (https://www.linuxquestions.org/questions/linux-newbie-8/reading-telemetry-data-and-applying-an-action-4175713061/)

Faki 06-06-2022 12:05 AM

Reading telemetry data and applying an action
 
I have the following line that reads telemetry data from a logfile when File: or + is encountered. I am setting the variable action to either continue or break, then running the command after the last + entry. It seems a bit confusing when the action gets executed upon reaching (*). What can I do to make the intent clear?

Code:

 
  action="continue"

  while IFS= read -r telemetry; do

    attrib="${telemetry%[[:blank:]]*}"
   
    case $attrib in
    ("File:")
              # "File:" field name has been reached.
              field="File" ; action="break" ;;
    ("+")
              # Detected the + continuation character
              [[ "$field" == "File" ]] && action="break" ;;
    (*)
              # action="continue"
              #  Ends current iteration, resuming at next iteration.
              #  Read next line in logfl, because "File" not yet reached.
              # action="break"
              #  Ends current iteration, exiting from the while loop.
              #  Exit logfl reading after end of field "File".
              $action ;;
    esac

  done < "$logfl"


michaelk 06-06-2022 05:55 AM

The *) matches anything so your loop will break for any line after action=break but you have set action="break" for both File:) and +) cases. If you want to break at a + then why not just break at the +) case.

Quote:

after the last + entry.
If you have multiple lines that are the same in the log file then without knowing all the details would not this find the first occurrence and not the last?

Faki 06-06-2022 01:21 PM

The action=break does not execute the action, only when reaching *) do the commands continue and break execute.

One can either have

Code:

File: Value
or

Code:

File: Value
+ Value
+ Value
+ Value

The task is to capture the values for the File field.

If + is encountered, I will read through all subsequent values until the last + in encountered.

michaelk 06-06-2022 02:34 PM

Does it work as expected?

Faki 06-06-2022 02:39 PM

I feel it is implemented in a weird way.

michaelk 06-06-2022 02:44 PM

If it is commented enough then you should be able to come back 2 years from now and be able to understand how it works. You could do the same thing with an if-then-else too.


All times are GMT -5. The time now is 04:24 PM.