1.stty命令介绍
stty
打印或更改terminal(终端)的设置
-a --all 用人类可读的方式,打印出所有现有设置
-g --save 用stty可读的方式,打印出所有现有设置
-F --file=DEVICE 打开或使用指定的“设备(DEVICE)”用以代替stdin
--help 帮助
--version 版本
# stty -a
speed 38400 baud; rows 60; columns 207; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; flush = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 -hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr icrnl ixon -ixoff -iuclc -ixany -imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
可以看到stty的当前设置,这里面的选项都是可以自已修改的。其中以’-‘开头的,为该选项被禁止的意思;如果没有’-‘,刚表示开启该选项。
stty具体用法:stty中文说明
2.stty的运用实例
(1). 暂停,按任意键后继续
#!/bin/sh
get_char()
{
SAVEDSTTY=`stty -g` #把当前stty的配置保存到SAVEDSTTY变量里
stty -echo #关闭回显,就是输入字符的时候,不显示在屏幕上
stty cbreak #击一下键盘输入一个字符就会响应,而不是等到输入回车按行响应
dd if=/dev/tty bs=1 count=1 2> /dev/null #把tty终端上接到的第1个字符丢入/dev/null, 也就是抛弃掉
stty -raw #允许规范输入方式
stty echo #开启回显
stty $SAVEDSTTY #还原当初的stty配置
}
echo ""
echo "Press any key to start...or Press Ctrl+c to cancel"
char=`get_char`
(2). 输入密码时不显示在屏幕上
#!/bin/sh
stty -echo
echo -n "Please set your password: "
read p
stty echo
echo -e "\nYour password is: $p"
(3). 限时输入
#!/bin/sh
stty -icanon min 0 time 100 while #min 0 表示最少0个字符, time 100 表示等待10s ,这里不是100s
do
echo "Please input a num :" read input
done
stty sane #恢复stty的默认设置