How can I add a counter to my script

1

I'm working on a script that gets information from a log called ip.txt and contains the following:

192.168.7.x OK
192.168.9.x OK
192.168.5.x OK

Looking for help, I managed to make it print a status depending on the file if all the IPs have "OK" then this "OK" if any of the IP has something other than "OK" then it is wrong.

filename="/home/ip.log" 
while read -r line 
do
  readline=$line
  if [[ $readline = *"OK"* ]]
  then
  result_output="BIEN"
  else
  result_output="MAL"
  fi
done < <(tail -n "+2" $filename)
echo $result_output

Now I would like to make that when the output is "BAD" determine how many lines were like a counter, for example when printing something like this "5 BAD"

What I did was add:

filename="/home/ip.log" 
while read -r line 
do
  **var=0**
  readline=$line
  if [[ $readline = *"OK"* ]]
  then
  result_output="BIEN"
  else
  result_output="MAL"**"$((var++))"**
  fi
done < <(tail -n "+2" $filename)
echo $result_output

But this is the only thing he does is print is "0" at the end of MAL

    
asked by Adrian Licon 04.01.2019 в 02:55
source

2 answers

1

Adapting your code a bit

#!/bin/bash

filename="/home/ip.log" 
while read -r line 
do
    readline=$line

    if [[ $readline != *"OK"* ]]; then
        errores=$((errores + 1))
    fi
done < <(tail -n "+2" $filename)

if [[ $errores > 1 ]]; then
    echo "MAL; $errores"
else
    echo "BIEN"
fi

What we do basically, is to handle an error counter, when in a line we do not find a OK we add one to this: errores=$((errores + 1)) , then simply, at the end of the cycle we check the number of errors.

On the other hand, I suggest again using awk , more or less as I did in your previous question:

awk '$2 !~ /OK/ {error += 1} END {if(error > 0){print "MAL:", error}else{print "BIEN"}}' /home/ip.log
    
answered by 04.01.2019 / 03:18
source
0

A smaller option to write would be:

#!/bin/bash

IP_LOG_FILE="./ip.log"
IP_LOG_FILE_OK="./ip_ok.log"
IP_LOG_FILE_NOPE="./ip_nope.log"

q_logs_total=$( { cat $IP_LOG_FILE; echo ''; } | wc -l | cut -d " " -f 1) 
# ^^^^^^ Supongo que no hay líneas vacías y cuento el total de líneas del archivo.
logs_ok=$( egrep '[[:space:]]OK$' $IP_LOG_FILE | tee $IP_LOG_FILE_OK )
# Busco el patrón " OK" y las coincidencias las guardo en un archivo logs_ok.log
logs_nope=$( egrep -v '[[:space:]]OK$' $IP_LOG_FILE | tee $IP_LOG_FILE_NOPE )
# Busco que NO cumpla el patrón " OK" y las coincidencias las guardo en un archivo 
# logs_nope.log
q_logs_ok=$( wc -l <<< "$logs_ok" )
q_logs_nope=$( wc -l <<< "$logs_nope" )
# Cuento la coincidencias de cada tipo de registro.
msg="Registros:$q_logs_total
logs_ok:$q_logs_ok
logs_nope:$q_logs_nope"
# Creo el mensaje de salida.

printf "$msg\n" | sed -r 's/(.*)(:)(.*)/||/g' | column -t -s : 
# Doy formato para que el mensaje se agradable.
# echo "$msg" # O sólo pon esto, si quieres que sea basico.

That, in a file of the form:

192.168.1.x OK
192.168.2.x OK
192.168.3.x OK
192.168.4.x OK
192.168.5.x NOPE
192.168.6.x OK
192.168.7.x NOPE
192.168.8.x OK
192.168.9.x OK
192.168.10.x NOPE

I would throw this.

$ ./make_logs.sh
Registros  |10|
logs_ok    |7|
logs_nope  |3|

Saving the records in two files called "ip_ok.log" and "ip_nope.log", this is in case there are too many ip's. After having these files, you could read them with a pager like most or less

And these files will have something like:

$ head ip_*
==> ip_nope.log <==
192.168.5.x NOPE
192.168.7.x NOPE
192.168.10.x NOPE

==> ip_ok.log <==
192.168.1.x OK
192.168.2.x OK
192.168.3.x OK
192.168.4.x OK
192.168.6.x OK
192.168.8.x OK
192.168.9.x OK
    
answered by 04.01.2019 в 04:05