Home > Software engineering >  How to write a Bash script that shows the range of 500-511 HTTP server errors in a file
How to write a Bash script that shows the range of 500-511 HTTP server errors in a file

Time:02-20

I am trying to write a bash script that will list and count the number of HTTP: 500 - 511 web error inside this file "ccc2022-02-19.txt" Inside every file there are several 500 errors ranging from HTTP 500, 501, 502, 503 up to 511.

Within the directory where this files are , there are 4 different type of files listed there daily but I am only interested on the files that starts with "ccc" because they are listed daily for example "ccc2022-02-19.txt", "ccc2022-02-20.txt" etc

Below is an example of the file content "ccc2022-02-19.txt"

10.32.10.181 ignore 19 Feb 2022 00:26:04 GMT 10.32.10.44 GET / HTTP/1.1 500 73 N 0 h 10.32.26.124 ignore 19 Feb 2022 00:26:06 GMT 10.32.10.44 GET / HTTP/1.1 501 73 N 0 h 10.32.42.249 ignore 19 Feb 2022 00:26:27 GMT 10.32.10.44 GET / HTTP/1.1 500 73 N 1 h 10.32.10.181 ignore 19 Feb 2022 00:26:34 GMT 10.32.10.44 GET / HTTP/1.1 302 73 N 0 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 503 73 N 1 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 502 73 N 1 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 502 73 N 1 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 504 73 N 1 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 511 73 N 1 h 10.32.26.124 ignore 19 Feb 2022 00:26:36 GMT 10.32.10.44 GET / HTTP/1.1 508 73

I have tried using this command

awk '{for(i=1;i<=NF;i ){if($i>=500 && $i<=511){print $i}}}' ccc2022-02-19.txt

which listed the numbers 500 -511 but I'm afraid that it is not giving only the HTTP response but grepped other number too like 50023, 503893 found inside the file.

To be specific, I just want to see only the HTTP errors. Please note that the file content above is just an example......

CodePudding user response:

I think this script might help

#!/bin/bash

ccc=500
while [ $ccc -le 511 ]
do 
  echo $ccc
  ccc=$(( $ccc  1 ))
  sleep 0.5
done

CodePudding user response:

You can try out this:

#!/bin/bash
CURRENTDATE=`date  "%Y-%m-%d"`
echo Today date is=${CURRENTDATE}
echo Looking for today file www${CURRENTDATE}.txt
echo "#####"

echo Start listing 500 response codes for file:ccc${CURRENTDATE}.txt
#awk '{print $3 " " $4 " " $5 " " $6 " " $11}' ccc${CURRENTDATE}.txt | grep 500
echo "I am not listing to reduce amount of lines per Slack limit"
echo Completed listing 500 response codes for file:ccc${CURRENTDATE}.txt
echo "#####"

  • Related