Chinaunix首页 | 论坛 | 博客
  • 博客访问: 251545
  • 博文数量: 38
  • 博客积分: 4010
  • 博客等级: 上校
  • 技术积分: 592
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-03 11:37
文章分类

全部博文(38)

文章存档

2011年(1)

2009年(31)

2008年(5)

2007年(1)

我的朋友

分类:

2008-09-09 17:12:26

  tput命令将通过 terminfo 数据库对您的终端会话进行初始化和操作。通过使用tput您可以更改几项终端功能,如移动或更改光标、更改文本属性,以及清除终端屏幕的特定区域。我们先看看它的常用参数:

(1)字符串输出参数设置 
  bel       警铃 
  blink     闪烁模式 
  bold      粗体 
  civis     隐藏光标 
  clear     清屏 
  cnorm     不隐藏光标 
  cup       移动光标到屏幕位置(x,y) 
  el        清除到行尾 
  ell       清除到行首 
  smso      启动突出模式 
  rmso      停止突出模式 
  smul      开始下划线模式 
  rmul      结束下划线模式 
  sc        保存当前光标位置 
  rc        恢复光标到最后保存位置 
  sgr0      正常屏幕 
  rev       逆转视图 
(2)数字输出参数设置 
  cols      列数目 
  ittab     设置宽度 
  lines     屏幕行数 
(3)布尔输出参数设置 
  chts      光标不可见 
  hs        具有状态行 

  下面的代码创建了一个基本菜单。此脚本介绍了如何在tput中使用本文中介绍的多个选项增强您的代码。
#!/bin/bash
trap 'get_window_size' WINCH # trap when a user has resized the window

_UNDERLINE_ON=`tput smul` # turn on underline
_UNDERLINE_OFF=`tput rmul` # turn off underline

get_window_size() {
_WINDOW_X=`tput lines`
_WINDOW_Y=`tput cols`

_FULL_SPACES=`echo ""|awk '
{
_SPACES = '${_WINDOW_Y}'
while (_SPACES-- > 0) printf (" ")
}'`
_FULL_UNDERLINE=`echo "${_UNDERLINE_ON}${_FULL_SPACES}${_UNDERLINE_OFF}"`

unset _FULL_SPACES
show_menu

return 0
}

set_color() {
tput clear
PS3="Enter Selection[1-9]:"
select _COLOR in "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" "Exit"
do
case ${REPLY} in
[1-8]) _X=`expr ${REPLY} - 1`;;
9) break;;
*) echo "Invalid Color"; continue;;
esac

if [[ ${1} = "b" ]]
then
tput setb ${_X}
else
tput setf ${_X}
fi
done
}

show_menu() {
while [[ -z ${_ANS} ]]
do
tput civis
tput clear

cat <<- EOF
Window Size: ${_WINDOW_X} / ${_WINDOW_Y}


Select => ${_UNDERLINE_ON} ${_UNDERLINE_OFF}

${_FULL_UNDERLINE}
B) Background Text Color
F) Foreground Text Color

X) Exit
EOF

tput rc
tput smul
tput cnorm

read _ANS
tput rmul

case ${_ANS} in
[Bb]) set_color "b";;
[Ff]) set_color "f";;
[Xx]) tput clear; exit;;
*)
echo -e "Invalid Selection: ${_ANS}\c"
sleep 2
;;
esac
unset _ANS
done
}

tput sgr0
tput civis
tput clear
tput cup 3 10
tput sc
tput cup 0 0

[[ -n ${_ANS} ]] && unset _ANS
get_window_size

exit 0
下面我们分析一下 shell 脚本。

设置解释脚本的方式。在本例中,要使用的 shell 为 Bash。为WINCH信号设置一个陷阱,同时指定get_window_size函数作为捕获到的信号的触发器。在设置了陷阱之后,定义两个变量以便稍后在脚本中键入时使用。

#!/bin/bash
trap 'get_window_size' WINCH # trap when a user has resized the window

_UNDERLINE_ON='tput smul' # turn on underline
_UNDERLINE_OFF='tput rmul' # turn off underline
创建一个名为get_window_size的函数用来确定行数和列数。此外,定义_FULL_UNDERLINE变量,设备的宽度(带有下划线)。

get_window_size() {
_WINDOW_X='tput lines'
_WINDOW_Y='tput cols'

_FULL_SPACES='echo ""|awk '
{
_SPACES = '${_WINDOW_Y}'
while (_SPACES-- > 0) printf (" ")
}''
_FULL_UNDERLINE='echo "${_UNDERLINE_ON}${_FULL_SPACES}${_UNDERLINE_OFF}"'

unset _FULL_SPACES
show_menu

return 0
}
创建一个名为set_color的函数来允许用户测试背景和前景文本颜色。

set_color() {
tput clear
PS3="Enter Selection[1-9]:"
select _COLOR in "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" "Exit"
do
case ${REPLY} in
[1-8]) _X='expr ${REPLY} - 1';;
9) break;;
*) echo "Invalid Color"; continue;;
esac

if [[ ${1} = "b" ]]
then
tput setb ${_X}
else
tput setf ${_X}
fi
done
}
创建一个名为show_menu的函数,通过此函数来演示设备的大小。此函数中演示的内容还包括:将光标转变为不可见,清除屏幕,打印文本,以及返回到保存的光标位置。

show_menu() {
while [[ -z ${_ANS} ]]
do
tput civis
tput clear

cat <<- EOF
Window Size: ${_WINDOW_X} / ${_WINDOW_Y}

Select => ${_UNDERLINE_ON} ${_UNDERLINE_OFF}

${_FULL_UNDERLINE}
B) Background Text Color
F) Foreground Text Color

X) Exit
EOF

tput rc
tput smul
tput cnorm

read _ANS
tput rmul

case ${_ANS} in
[Bb]) set_color "b";;
[Ff]) set_color "f";;
[Xx]) tput clear; exit;;
*)
echo -e "Invalid Selection: ${_ANS}\c"
sleep 2
;;
esac
unset _ANS
done
}
接下来,设置一些基本的光标属性。首先,可以使用sgr0清除所有属性。光标将转换为不可见,并且屏幕将被清除。不可见的光标现在移动到 (3,10),此位置将被保存,然后光标将移动到 (0,0)(左上角)。

tput sgr0
tput civis
tput clear
tput cup 3 10
tput sc
tput cup 0 0
最后,调用get_window_size函数获取窗口大小,进而调用function show菜单。

[[ -n ${_ANS} ]] && unset _ANS
get_window_size

exit 0

将tput引入 UNIX 中的 shell 脚本可以改善脚本的外观。在 UNIX 中可以通过数百种方法来完成一项任务,为何不为您的方法增加一些色彩和个性化因素呢?学习tput非常容易,并且可能对脚本是非常有效的;用户将从更多地控制屏幕的外观方面获益。关于您可以使用tput做些什么,本文仅起到了抛砖引玉的作用。通过tput和极少的工作量,您就可以创建看起来非常漂亮而全面的菜单驱动的 shell 脚本!

阅读(1387) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~