how to refresh the variable output in multiple lines without screen clear

109 Views Asked by At

I'm trying to monitor a log file then display the count of specific request calls, the output will show the header line which has the names of each request "PUT GET HEAD" and below that the count of requests of each node per line. I need to refresh the output of the variables that shows the count each 1 sec

the problem with the below code, the script keeps on deleting previous lines and go up to the top of the screen, while the output should be fixed and only the number of request should refresh

#!/usr/bin/bash

#variables
MYIP=$(ifconfig eth1|grep -w 'inet'| awk '{print $2}')
BASEIP=$(echo $XIP | cut -d"." -f-3)

MONPUT=$(cat $HTTPLOG  | grep -w "PUT" |awk '{print $9}'| grep -E "20"| wc -l)
MONHEAD=$(cat $HTTPLOG  | grep -w "HEAD" | awk '{print $9}'| grep -E "20" |wc -l)
MONGET=$(cat $HTTPLOG  | grep -w "GET" | awk '{print $9}'| grep -E "20" |wc -l)
MONDEL=$(cat $HTTPLOG  | grep -w "DELETE" | awk '{print $9}'| grep -E "20" |wc -l)


# header 
printf "%15s" "Node " |tr '\n' '\t'
printf "%15s"  'PUT'  | tr '\n' '\t'
printf "%15s" 'HEAD' | tr '\n' '\t'
printf "%15s"  'GET'  | tr '\n' '\t'
printf "%15s"  'DELETE'  | tr '\n' ' '

# starting the loop

while [[ true ]]
do 
i=(1)

for (( x=1; x<=3; x++))
do 
((i = i + 1))
NODEIP="$BASEIP.$i"
ssh $NODEIP printf "%15s" "$GETNAME" "$MONPUT" "$MONHEAD" "$MONGET" "$MONDEL"
done

echo -ne "\033[3A\r"
done

here is the expected output:

Node PUT - HEAD - GET - DELETE node-2 1849 2183 41387 0

#

node-3 5339 8534 40838 0

#

node-4 1829 2196 46503 0

#

expected output: should show the same as above while the numbers are refreshed every 1 sec

1

There are 1 best solutions below

0
tale852150 On

I agree with UtLox that, generally speaking, ncurses is your best option. With that in mind, please take a look at bashsimplecurses:

Git repo

Tutorial

Basic example