脚本输入
来源:互联网 发布:java json对象转数组 编辑:程序博客网 时间:2024/06/08 06:19
在使用tput前,需要在脚本或命令行中使用 tput命令初始化终端。
$ tput init
tput产生三种不同的输出:字符型、数字型和布尔型(真/假)。以下分别介绍其使用功
能。
字符串输出选项:
bel 警铃
blink 闪烁模式
bold 粗体
civis 隐藏光标
clear 清屏
cnorm 不隐藏光标
cup 移动光标到屏幕位置( x,y)
el 清除到行尾
ell 清除到行首
smso 启动突出模式
rmso 停止突出模式
smul 开始下划线模式
rmul 结束下划线模式
sc 保存当前光标位置
rc 恢复光标到最后保存位置
sgr0 正常屏幕
rev 逆转视图
数字输出选项:
cols 列数目
it tab设置宽度
lines 屏幕行数
布尔类型输出:
chts 光标不可见
hs 具有状态行
举例:
#!/bin/bash
status='tput hs'
if $status; then
echo "your terminal has a status line"
else
echo "your terminal doesn't has a status line"
fi
改变属性:
#!/bin/bash
#status='tput hs'
#if $status; then
# echo "your terminal has a status line"
#else
# echo "your terminal doesn't has a status line"
#fi
BOLD=`tput bold` #设置粗体
REV=`tput rev` #逆转视图
NORMAL=`tput srg0` #正常屏幕
CURSOR_OFF=`tput civis` #隐藏光标
CURSOR_ON=`tput cnorm` #不要隐藏光标
tput init
echo $CURSOR_OFF
echo "$BOLD welcome to the quite places $NORMAL"
echo -e "\n$REV we are open 7 days a week $NORMAL"
echo $CURSOR_ON
光标位置
可以用tput将光标放在屏幕任意位置。格式为:
cup r c
r为从上至下屏幕行数, c为穿过屏幕列数。
举例:
function gotoxy()
{
_Row=$1
_Col=$2
tput cup $_Row $_Col
}
clear
gotoxy 1 5
echo -n "Please enter your first name:"
read firstname
gotoxy 3 5
echo -n "Please enter your last name"
read lastname
echo "your name is $firstname, $lastname"
如何在屏幕中间显示内容呢?
正好封装两个函数
function gotoxy()
{
clear
_Row=$1
_Col=$2
tput cup $_Row $_Col
}
#
#clear
#gotoxy 1 5
#echo -n "Please enter your first name:"
#read firstname
#gotoxy 3 5
#echo -n "Please enter your last name"
#read lastname
#echo "your name is $firstname, $lastname"
function showContent()
{
_Row=$1
_TEXT=$2
LEN=`echo $_TEXT | wc -c`
COLS=`tput cols`
_NEW_COL=`expr \( $COLS - $LEN \) / 2`
gotoxy $_Row $_NEW_COL
echo $_TEXT
}
showContent 13 "Hello world"
2.命令infocmp从terminfo数据库中抽取终端信息,如果要查看终端定义文件的完整列表,可使用命令:
$ infocmp $TERM
3.在脚本中使用颜色
下面将使用的颜色是ANSI标准颜色,并不是所有颜色都适合于所有系统。下面列出了大部分常用颜色。
前景色:
30 黑色
31 红色
32 绿色
33 黄(棕)色
34 蓝色
35 紫色
36 青色
37 白色(灰色)
背景色:
40 黑色
41 红色
42 绿色
43 黄(棕)色
44 蓝色
45 紫色
46 青色
47 白色(灰色)
显示颜色的格式为:
[background_num; foreground_num m
背景色为白色 前景色为蓝色
echo -e "\033[47;34m"
一个简单的示例:
#!/bin/bash
tput init
mydate=`date +%D`
function color()
{
case $1 in
black_green)
echo -e '\033[40;32m'
;;
black_white)
echo -e '\033[40:37m'
;;
black_yellow)
echo -e '\033[40:33m'
;;
black_cyan)
echo -e '\033[40;36m'
;;
black_red)
echo -e '\033[40:31m'
;;
esac
}
function gotoxy()
{
row=$1
col=$2
text=$3
tput cup $row $col
echo -n $text
}
function center()
{
string=$1
row=$2
len=`echo $string | wc -c`
col=`tput cols`
cal_col=`expr $col - $len`
new_col=`expr $cal_col / 2`
tput cup $row $new_col
echo -n $string
}
clear
color black_yellow
gotoxy 2 3 "user: $LOGNAME"
color black_cyan
center "Add a new wrap drive to a star ship" 3
echo -e '\f\f'
center "___________________________________" 4
color black_yellow
gotoxy 5 1 "___________________________________________"
#gotoxy 7 1 "___________________________________________"
gotoxy 21 1 "___________________________________________"
center "Start Date $mydate " 22
gotoxy 23 1 "___________________________________________"
color black_green
gotoxy 6 6 "What's your name: "
read name
gotoxy 8 6 "Student's number:"
read number
gotoxy 10 6
echo -n "Student's age:"
read age
gotoxy 12 6
echo -n "Student's sex: "
read sex
gotoxy 16 4
echo -n "Student Info: $name,$number,$age,$sex"
sleep 4
color black_white
clear
还有一个有点小问题的例子(也贴出来,请教一下哪里错了):
#!/bin/bash
trap "" 2 3 15
mydate=`date +%d-%m-%y`
host=`hostname -s`
user='Jack'
user_txt="user.txt"
hold="hold.$$"
function color()
{
case $1 in
black_green)
echo -e '\033[40;32m'
;;
black_white)
echo -e '\033[40:37m'
;;
black_yellow)
echo -e '\033[40:33m'
;;
black_cyan)
echo -e '\033[40;36m'
;;
black_red)
echo -e '\033[40:31m'
;;
esac
}
function get_char()
{
savetty=`stty -g`
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -cbreak
stty $savetty
}
function setCursor()
{
CURSOR_OFF=`tput civis`
CURSOR_ON=`tput cnorm`
opt=$1
case $opt in
on)
echo '$CURSOR_ON'
;;
off)
echo '$CURSOR_OFF'
;;
*)
return 1
;;
esac
}
function check()
{
color black_red
echo -n "Have no power to use this function"
color black_green
}
function user_level()
{
while read LINE
do
case $LINE in
\#*);;
*)
echo $LINE >>$hold
;;
esac
done < $user_txt
FOUND=false
while read MENU_USER PRIV #读取两个数
do
if ["$MENU_USER" = "$user"]
then
FOUND=true
case $PRIV in
yes|YES)
return 0
;;
no|NO)
return 1
;;
esac
else
continue
fi
done < $hold
if [ "$FOUNE" = "false" ] ;then
echo "sorry $user you have not been authority to use this menu"
exit 1
fi
}
function destroy()
{
color black_white
setCursor on
rm *.$$
exit 0
}
tput init
if user_level; then
access="access mode is high"
else
access="access mode is normal"
fi
tput init
while :
do
tput clear
color black_green
cat <<MYDAY
$access
__________________________________________________
User: $user Host: $host Data:$mydata
__________________________________________________
1: Add a record
2: View a record
3: Page all records
4: Chane a record
5: Delete a record
P: Print all records
H: Help screen
O: Exit menu
____________________________________________________
MYDAY
color black_cyan
echo -e -n "Your choice[1 2 3 4 5 P H O]:"
read choice
choice=`get_char`
case $choice in
1) ls ;;
2) vi ;;
3) who ;;
4) if user_level; then
ls -l | wc
else
check
fi;;
5) if user_level; then
sort /wtc/passwd
else
check
fi;;
P) ;;
H) ;;
E) break ;;
esac
done
参考书籍: shell脚本编程
QQ交流群: 204944806
- 脚本输入
- 输入验证脚本
- bat 脚本自动输入
- shell脚本密码输入
- 只能输入数字的脚本
- javascript限制脚本输入大全
- javascript限制脚本输入大全
- javascript限制输入脚本大全
- 只能输入汉字js脚本
- linux脚本判断输入参数
- 只能输入汉字js脚本
- unix expect自动输入脚本
- bash脚本输入密码不回显
- 网页脚本输入这么简单
- python脚本输入密码判断
- Linux自动输入密码脚本
- shell 脚本 输入参数 学习
- 用javascript输入一个脚本,以确认用户输入
- failed to load selinux policy
- Android NDK Sqlite3 加密技术
- 算法设计之最长公共子序列(LCS)问题
- 我的编程之路(二十六) 国庆假期(一)
- 计算机网络--梳理
- 脚本输入
- 堆内存和栈内存(待整理……)
- HDU 2896 病毒侵袭 (AC自动机)
- 【Java】【TIJ】用import改变行为,实现条件编译功能
- 开机的公开和地方就会离开的房间了回来可减肥的理发店
- 【编程拾遗】C++的static成员函数与单例模式
- Majority Element问题
- View编程:invalidate()源码分析
- LaTeX 使用经验之几个小问题