我找到了现有答案中未提及的另一种解决方案。我正在为 openwrt 开发程序,tput
默认情况下不可用。下面的解决方案受到Missing tput和Cursor Movement的启发。
- Position the Cursor:
\033[<L>;<C>H
Or
\033[<L>;<C>f
puts the cursor at line L and column C.
- Move the cursor up N lines:
\033[<N>A
- Move the cursor down N lines:
\033[<N>B
- Move the cursor forward N columns:
\033[<N>C
- Move the cursor backward N columns:
\033[<N>D
- Clear the screen, move to (0,0):
\033[2J
- Erase to end of line:
\033[K
- Save cursor position:
\033[s
- Restore cursor position:
\033[u
至于你的问题:
var1="pending"
var2="pending"
var3="pending"
print_status () {
# add \033[K to truncate this line
echo "Status of Item 1 is: "$var1"\033[K"
echo "Status of Item 2 is: "$var2"\033[K"
echo "Status of Item 3 is: "$var3"\033[K"
}
while true; do
print_status
sleep 1
printf "\033[3A" # Move cursor up by three line
done