linux - bash:如何在同一位置回显字符串

标签 linux bash shell unix echo

我构建了我的网络服务器,我正在尝试进行测试。所以我用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

这很好用,但我更喜欢在终端的相同位置制作所有 echo
我读过这个:How to show and update echo on same line

所以我为 echo 添加了 -ne 但它似乎没有按预期工作。
curl 的消息仍然可以将 echo 推开。

这是我需要的:

============== current time =============== ---\
1   <------ this number keeps updating      ----> the 3 lines stay here
=========================================== ---/
Here is the messages of `curl`, which are showing as normal way

最佳答案

还有另一种选择,在写入标准输出之前定位光标。

您可以根据需要设置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

关于linux - bash:如何在同一位置回显字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44666308/

相关文章:

linux - 如何从 4400 万行文件中随机选择 1 万行

linux - 如何更新终端上特定行的文本?

node.js - 如何运行包括 Node.js 服务器的多个 shell 命令? Ubuntu

linux - korn shell 将文件名分配给数组

c - 理解 crypt() 在 MD5 情况下的输出

linux - Unix 中 "cat -A"命令选项是什么意思

linux - 如何在 Linux 中杀死 R 的并行程序

linux - 如何在linux中使用我自己的动态库(Makefile)

python - 将 python 输出提供给 whiptail

bash - 带 bash 的条件否定