4

I built my web server and I'm trying to do a test. So I simulate many requests with bash script:

i=0
while [ $i -lt 20 ]; do
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done

This works well but I prefer to make all of echo at the same position on the terminal.
I've read this: How to show and update echo on same line

So I add -ne for echo but it doesn't seem to work as expected.
The messages of curl can still push the echo away.

This is what I need:

============== current time =============== ---\
1   <------ this number keeps updating      ----> the 3 lines stay here
=========================================== ---/
Here is the messages of `curl`, which are showing as normal way
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Yves
  • 11,597
  • 17
  • 83
  • 180

4 Answers4

6

There's another option, to position the cursor before you write to stdout.

You can set x and y to suit your needs.

#!/bin/bash

y=10
x=0
i=0
while [ $i -lt 20 ]; do
    tput cup $y $x
    echo ''
    echo ''
    echo ''
    echo '============== current time ==============='
    echo $i
    echo '==========================================='
    echo ''
    curl -i http://www.example.com/index?key=abceefgefwe
    i=$((i+1))
done
codeforester
  • 39,467
  • 16
  • 112
  • 140
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

You could add a clear command at the beginning of your while loop. That would keep the echo statements at the top of the screen during each iteration, if that's what you had in mind.

  • Maybe you could pipe curl to head/tail -n 30 to control the number of lines output and keep it from printing enough to push the header off the screen? – Adam Schettenhelm Jun 21 '17 at 03:35
0

When I do this sort of thing, rather than using curses/ncurses or tput, I just restrict myself to a single line and hope it doesn't wrap. I re-draw the line every iteration.

For example:

i=0
while [ $i -lt 20 ]; do
  curl -i -o "index$i" 'http://www.example.com/index?key=abceefgefwe'
  printf "\r==== current time: %2d ====" $i
  i=$((i+1))
done

If you're not displaying text of predictable length, you might need to reset the display first (since it doesn't clear the content, so if you go from there to here, you'll end up with heree with the extra letter from the previous string). To solve that:

i=$((COLUMNS-1))
space=""
while [ $i -gt 0 ]; do
  space="$space "
  i=$((i-1))
done
while [ $i -lt 20 ]; do
  curl -i -o "index$i" 'http://www.example.com/index?key=abceefgefwe'
  output="$(head -c$((COLUMNS-28))) "index$i" |head -n1)"
  printf "\r%s\r==== current time: %2d (%s) ====" "$space" $i "$output"
  i=$((i+1))
done

This puts a full-width line of spaces to clear the previous text and then writes over the now-blank line with the new content. I've used a segment of the first line of the retrieved file up to a maximum of the line's width (counting the extra text; I may be one off somewhere). This would be cleaner if I could just use head -c$((COLUMNS-28)) -n1 (which would care about the order!).

Adam Katz
  • 14,455
  • 5
  • 68
  • 83
0

Please try below..

#!/bin/bash

echo -e "\033[s" 

echo -e "\033[u**Your String Here**"
helvete
  • 2,455
  • 13
  • 33
  • 37