如何解决是否可以在不清除屏幕的情况下替换行?
printf "line one\n"
printf "line two\n"
这些打印输出之后,我想替换第一行并打印其他内容
(不使用clear
)。我已经尝试过以下命令:
printf "line one\n"
printf "line two\r"
这不是我想要的,因为它代替了最后一行line two
,而不是line one
。
我想做什么:
printf "line one\n"
printf "line two\n"
sleep 0.5
somecode "line three"
我想要的输出:
line three
line two
解决方法
这可以通过flatMap
完成,例如:
tput
最初将打印:
EraseToEOL=$(tput el) # save control code for 'erase to end of line'
tput sc # save pointer to current terminal line
printf "line one - a long line\n"
printf "line two\n"
sleep 0.5
tput rc # return cursor to last cursor savepoint (`tput sc`)
printf "line three${EraseToEOL}\n" # print over `line one - a long line`; print `$EraseToEOL` to clear rest of line (in case previous line was longer than `line three`)
printf "\n" # skip over 'line two'
然后,在休眠0.5秒后line one - a long line # `tput sc` will point to this line
line two
<cursor> # cursor is left sitting on this line
将导致光标在执行最后2x tput rc
条命令之前向上移动2行:
printf
,
您可以通过打印特殊的转义序列在bash脚本中移动光标,请尝试以下代码:
#!/bin/bash
# print first line
printf "first line is long\n"
# print second line
printf "line 2\n"
sleep 1
# move cursor two steps UP
printf "\033[2A"
# print line 3 (without \n)
printf "line #3"
# clear rest of first line
# and move cursor two steps down
printf "\033[K\r\033[2B"
有关ANSI转义序列的更多信息:https://tldp.org/HOWTO/Bash-Prompt-HOWTO/c327.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。