gpt4 book ai didi

Bash 动态命令行输出

转载 作者:行者123 更新时间:2023-11-29 09:13:57 28 4
gpt4 key购买 nike

我想知道如何在 shell 中获得动态输出。我的意思是您使用“top”命令或 wget 下载的状态栏获得的输出类型:命令行输出更改时没有任何换行符。

我的具体需求是从脚本中获得反馈(需要大量操作才能执行),而无需为每个操作反馈换行,例如在非常大的网络中发送 ping 的发现脚本并且可以动态地告诉发现的状态。

(不,我不能使用 nmap :D 无论如何它是为了学习目的)

谢谢!

最佳答案

“动态”显示只是一系列打印语句。其中一些打印语句打印屏幕控制代码,这些代码可以移动光标或重新绘制屏幕的某些部分。事实上的标准框架是 ncurses .要从 shell 脚本中访问,您需要 tput .

这是一个简单的“动画”微调器。

#!/bin/sh

# Restore cursor and move to new line when terminated
trap 'tput cnorm; echo' EXIT
trap 'exit 127' HUP INT TERM

# Make text cursor invisible
tput civis
# Save cursor position
tput sc
while true; do
for char in '-' '\' '|' '/'; do
# Back to saved position
tput rc
printf "%s" "$char"
sleep 1
done
done

关于Bash 动态命令行输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31156508/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com