使用bash获取实时代码执行更新

时间:2020-10-25 14:01:16

标签: linux bash scripting real-time

通常,我们会看到程序正在运行并实时显示运行了多长时间

示例 [#] Scanning is running from 1min 30sec

该值将动态更改,直到脚本完成。

我们如何在bash中做到这一点!

1 个答案:

答案 0 :(得分:1)

通过使用带回车符但没有换行符的printf

count=0
SECONDS=0
while ((count < 10)); do
    # computation code goes here
    # just use a sleep for demonstration
    sleep 1

    printf "\rRunning for %d seconds" $SECONDS
    ((count++))
done
echo # just to add a newline in the output

SECONDS是bash中的special variable