【发布时间】:2013-01-10 13:42:25
【问题描述】:
据我所知,可以使用 echo 中的退格序列将光标向左移动。但是有没有可能使用 echo 来改变光标的垂直位置?
【问题讨论】:
标签: bash
据我所知,可以使用 echo 中的退格序列将光标向左移动。但是有没有可能使用 echo 来改变光标的垂直位置?
【问题讨论】:
标签: bash
本节介绍 ANSI 转义序列:
例子:
echo -en "\033[s\033[7B\033[1;34m 7 lines down violet \033[u\033[0m"
echo -en "\033[s\033[7A\033[1;32m 7 lines up green \033[u\033[0m"
本节介绍 tput 实用程序:
如需演示,请参阅终端中的浮动时钟:
取自http://www.cyberciti.biz/tips/spice-up-your-unix-linux-shell-scripts.html的示例脚本:
#!/bin/bash
tput clear # clear the screen
tput cup 3 15 # Move cursor to screen location X,Y (top left is 0,0)
tput setaf 3 # Set a foreground colour using ANSI escape
echo "XYX Corp LTD."
tput sgr0
tput cup 5 17
tput rev # Set reverse video mode
echo "M A I N - M E N U"
tput sgr0
tput cup 7 15; echo "1. User Management"
tput cup 8 15; echo "2. Service Management"
tput cup 9 15; echo "3. Process Management"
tput cup 10 15; echo "4. Backup"
tput bold # Set bold mode
tput cup 12 15
read -p "Enter your choice [1-4] " choice
tput clear
tput sgr0
tput rc
【讨论】:
使用echo 将限制您使用特定的终端类型。最好使用tput。
tput cup 10 4; echo there
将光标放在第 10 行第 4 列并在该位置打印“there”。
对于更多基本动作,您可以使用tput cub1 向左移动,tput cuf1 向右移动,tput cuu1 向上移动,tput cud1 向下移动光标。
【讨论】:
是的,正如 miku 所指出的,ANSI 终端转义,或者更简洁的 tput 应该可以在任何终端上工作:
tput cuu 1 # move cursor up 1 position
nudgeup=`tput cuu 2` # save it for later
nudgeleft=`tput cub 2`
echo -n $nudgeup $nudgeleft
请参阅 BASH 提示操作方法中 ANSI 部分之后的几个部分。
tput 还会返回一个退出代码,告诉您某些内容是否不受支持。
【讨论】: