简体   繁体   English

bash:如何在相同位置回显字符串

[英]bash: How to echo strings at the same position

I built my web server and I'm trying to do a test. 我建立了Web服务器,正在尝试进行测试。 So I simulate many requests with bash script: 所以我用bash脚本模拟了许多请求:

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. 这很好用,但是我更喜欢在终端上的相同位置进行所有echo
I've read this: How to show and update echo on same line 我读过这里: 如何在同一行上显示和更新echo

So I add -ne for echo but it doesn't seem to work as expected. 因此,我为echo添加了-ne ,但它似乎没有按预期工作。
The messages of curl can still push the echo away. curl的信息仍然可以将echo推开。

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

There's another option, to position the cursor before you write to stdout. 还有另一种选择,在写入标准输出之前将光标定位。

You can set x and y to suit your needs. 您可以根据需要设置xy

#!/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

You could add a clear command at the beginning of your while loop. 您可以在while循环的开头添加一条clear命令。 That would keep the echo statements at the top of the screen during each iteration, if that's what you had in mind. 如果您要记住的话,这将在每次迭代期间将echo语句保持在屏幕顶部。

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. 当我做这种事情时,我没有使用curses / ncurses或tput ,而是将自己限制为一行,并希望它不会被包装。 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). 如果你没有显示预测的长度的文本,则可能需要先重新设置显示器(因为它没有明确的内容,所以如果你去therehere ,你会最终heree从额外的信前一个字符串)。 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!). 如果我可以只使用head -c$((COLUMNS-28)) -n1 (这会关心顺序!),这会更干净。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM