stty命令的用法全解

来源:互联网 发布:慧通网络直播聊天室 编辑:程序博客网 时间:2024/05/16 12:57
UNIX系统的命令很多,但是巧妙使用命令的方法更多。随着经验的积累和观察学习其他用户的实践,我们也可学会解决特殊问题的方法。这里谈谈自己使用UNIX系统中stty命令的一些体会,希望能与你共享。 

1.stty(set tty,设置tty)命令用于检查和修改当前注册的终端的通信参数。UNIX系统为键盘的输入和终端的输出提供了重要的控制手段,可以通过stty命令对特定终端或通信线路设置选项。

可以在stty命令中使用-a查看当前注册终端的设置情况。 
$ stty -a 
Speed 9600 baud; line =0;intr= DEL;quit = ^;erase = ^h;kill =^U; eof = ^d;start=^q;stop=^s 
-parenb -parodd cs8 -cstopb -hupcl cread -clocal -loblk 
-ignbrk brkint ignpar -parmrk -inpck istrip -inlcr -igncr icrnl -iuclc 
ixon ixany -ixoff 
isig icanon -xcase echo echoe echok -echonl -noflsh 
opost -olcuc onlcr -ocrnl -onocr onlret ofill ofdel tab3 

需要时,这里显示的各种选择都可以重新配置。每种终端选项都有自己的名字(如ixon),其中大多数要么被设置要么被清除。在stty中相应选项前冠以负号(-),该选项被清除(禁止);如果无负号,该选项被设置。 
当希望修改各选择的值时,应在stty命令中正确书写相应选项。如命令 

$ stty -ixon 
将流控制设置为OFF,而命令 
$ stty ixon 
则将其置为ON。
在stty命令中可以同时设置多个选项: 
$ stty ixon 1200 
该命令将终端设置为1200波特,并将流控制置为ON。 

当选项的列表较长时,应当小心所做的修改造成会话的混乱。典型的情况下,终端在设置不正确时完全不能工作。经常出现的现象是列输出显示或用户的击键不能被shell正确接受。有时NEWLINE可能只显示或走一行但不回行,即用户输入的NEWLINE符不能被shell正确读取。建立新注册标识的过程通常建立了正确的stty选项,在日常使用中很少需要修改。但如果变量TERM的设置不正确或用户希望使用新的终端或通信渠道(如局域网),则需要修改stty的选项。
如果你在某个机器中注册后终端操作正常,但后来因故发生了混乱,那么最佳的解决方法是先注销然后再向系统注册。因为注册时系统会重新设置相应选项。还有一种不太严格的解决办法是: 
$ stty sane 
命令sane最常出现在大多数终端的stty选项的集合中。它并不改变通信信道的速率,但当终端发生混乱时,通常能够产生有益的效果。命令sane可能使全屏幕应用程序(如vi)不能正确工作,而且在设置了选项sane之后,如果不能解决任何问题,就应立即注销,然后再注册。如果终端在注册后依然不能正确工作,用户的注册可能是不成功的。如果终端已经混乱,NEWLINE键不能结束命令行,可以用CTRL-J代替NEWLINE直到用户执行stty sane。此时,再用CTRL-J终止stty sane,使系统恢复正常。 

stty命令的几项主要选项功能为: 
选项parenb使终端进行奇偶校验,-parenb则是禁止校验; 
选项cs5、cs6、cs7和cs8分别将字符大小设为5、6、7和8比特; 
选项300、600、1200、2400、4800、9600和19200设置波特率; 
cstopb和-cstopb分别设置两个或一个停止位; 
tabs使系统使用制表符而不是空格序列,因此减少了输出量,选项-tabs仅使用空格,当终端不能正确处理制表符(tab)时应使用该项。 

2. stty还可用于控制一些具有特殊控制功能的键。系统可能已经使用CTRL-D作为文件结束符,但你可能喜欢使用CTRL-C。因此可以使用stty命令改变之: 
$ stty eof c 
其中c可以是你喜欢的其它特殊控制字符。 

可以直接输入控制字符,在其前面冠以反斜线和脱字符( ^)。命令 
$ stty eof ^c 
将CTRL-C设置为文件结束符。
这种语法形式还可以用于修改删除符(通常为退格键BackSpace)和中断符(通常为DEL键)。若希望将删除符改为BACKSPACE,可以使用下列命令: 
$ stty erase ^h 
可以设置一个会话期,使得当自己键入一个退格键时,系统用退格、删除和退格序列响应。此时可以看到用退格键覆盖的字符从显示中消失了,这样更符合计算机的惯例。使用命令 
$ stty echoe 
可使这一特性设置为ON。 

终端方式及其管理是UNIX系统中比较困难的内容之一,系统的用户手册中有stty选项的全部清单。在进行尝试之前,应当查阅手册,仔细分析,以免给你的工作带来麻烦。

stty --help
帮助信息:
  or:  stty [-F DEVICE] [--file=DEVICE] [-a|--all]
  or:  stty [-F DEVICE] [--file=DEVICE] [-g|--save]
Print or change terminal characteristics.
  -a, --all          print all current settings in human-readable form
  -g, --save         print all current settings in a stty-readable form
  -F, --file=DEVICE  open and use the specified DEVICE instead of stdin
      --help     ??????????
      --version  ?????????
Optional - before SETTING indicates negation.  An * marks non-POSIX
settings.  The underlying system defines which settings are available.
Special characters:
* dsusp CHAR    CHAR will send a terminal stop signal once input flushed
   eof CHAR      CHAR will send an end of file (terminate the input)
   eol CHAR      CHAR will end the line
* eol2 CHAR     alternate CHAR for ending the line
   erase CHAR    CHAR will erase the last character typed
   intr CHAR     CHAR will send an interrupt signal
   kill CHAR     CHAR will erase the current line
* lnext CHAR    CHAR will enter the next character quoted
   quit CHAR     CHAR will send a quit signal
* rprnt CHAR    CHAR will redraw the current line
   start CHAR    CHAR will restart the output after stopping it
   stop CHAR     CHAR will stop the output
   susp CHAR     CHAR will send a terminal stop signal
* swtch CHAR    CHAR will switch to a different shell layer
* werase CHAR   CHAR will erase the last word typed
Special settings:
  N             set the input and output speeds to N bauds
* cols N        tell the kernel that the terminal has N columns
* columns N     same as cols N
   ispeed N      set the input speed to N
* line N        use line discipline N
   min N         with -icanon, set N characters minimum for a completed read
   ospeed N      set the output speed to N
* rows N        tell the kernel that the terminal has N rows
* size          print the number of rows and columns according to the kernel
   speed         print the terminal speed
   time N        with -icanon, set read timeout of N tenths of a second
Control settings:
   [-]clocal     disable modem control signals
   [-]cread      allow input to be received
* [-]crtscts    enable RTS/CTS handshaking
   csN           set character size to N bits, N in [5..8]
   [-]cstopb     use two stop bits per character (one with `-')
   [-]hup        send a hangup signal when the last process closes the tty
   [-]hupcl      same as [-]hup
   [-]parenb     generate parity bit in output and expect parity bit in input
   [-]parodd     set odd parity (even with `-')
Input settings:
   [-]brkint     breaks cause an interrupt signal
   [-]icrnl      translate carriage return to newline
   [-]ignbrk     ignore break characters
   [-]igncr      ignore carriage return
   [-]ignpar     ignore characters with parity errors
* [-]imaxbel    beep and do not flush a full input buffer on a character
   [-]inlcr      translate newline to carriage return
   [-]inpck      enable input parity checking
   [-]istrip     clear high (8th) bit of input characters
* [-]iutf8      assume input characters are UTF-8 encoded
* [-]iuclc      translate uppercase characters to lowercase
* [-]ixany      let any character restart output, not only start character
   [-]ixoff      enable sending of start/stop characters
   [-]ixon       enable XON/XOFF flow control
   [-]parmrk     mark parity errors (with a 255-0-character sequence)
   [-]tandem     same as [-]ixoff
Output settings:
* bsN           backspace delay style, N in [0..1]
* crN           carriage return delay style, N in [0..3]
* ffN           form feed delay style, N in [0..1]
* nlN           newline delay style, N in [0..1]
* [-]ocrnl      translate carriage return to newline
* [-]ofdel      use delete characters for fill instead of null characters
* [-]ofill      use fill (padding) characters instead of timing for delays
* [-]olcuc      translate lowercase characters to uppercase
* [-]onlcr      translate newline to carriage return-newline
* [-]onlret     newline performs a carriage return
* [-]onocr      do not print carriage returns in the first column
   [-]opost      postprocess output
* tabN          horizontal tab delay style, N in [0..3]
* tabs          same as tab0
* -tabs         same as tab3
* vtN           vertical tab delay style, N in [0..1]
Local settings:
   [-]crterase   echo erase characters as backspace-space-backspace
* crtkill       kill all line by obeying the echoprt and echoe settings
* -crtkill      kill all line by obeying the echoctl and echok settings
* [-]ctlecho    echo control characters in hat notation (`^c')
   [-]echo       echo input characters
* [-]echoctl    same as [-]ctlecho
   [-]echoe      same as [-]crterase
   [-]echok      echo a newline after a kill character
* [-]echoke     same as [-]crtkill
   [-]echonl     echo newline even if not echoing other characters
* [-]echoprt    echo erased characters backward, between `\' and '/'
   [-]icanon     enable erase, kill, werase, and rprnt special characters
   [-]iexten     enable non-POSIX special characters
   [-]isig       enable interrupt, quit, and suspend special characters
   [-]noflsh     disable flushing after interrupt and quit special characters
* [-]prterase   same as [-]echoprt
* [-]tostop     stop background jobs that try to write to the terminal
* [-]xcase      with icanon, escape with `\' for uppercase characters
Combination settings:
* [-]LCASE      same as [-]lcase
   cbreak        same as -icanon
   -cbreak       same as icanon
   cooked        same as brkint ignpar istrip icrnl ixon opost isig
                 icanon, eof and eol characters to their default values
   -cooked       same as raw
   crt           same as echoe echoctl echoke
   dec           same as echoe echoctl echoke -ixany intr ^c erase 0177
                 kill ^u
* [-]decctlq    same as [-]ixany
   ek            erase and kill characters to their default values
   evenp         same as parenb -parodd cs7
   -evenp        same as -parenb cs8
* [-]lcase      same as xcase iuclc olcuc
   litout        same as -parenb -istrip -opost cs8
   -litout       same as parenb istrip opost cs7
   nl            same as -icrnl -onlcr
   -nl           same as icrnl -inlcr -igncr onlcr -ocrnl -onlret
   oddp          same as parenb parodd cs7
   -oddp         same as -parenb cs8
   [-]parity     same as [-]evenp
   pass8         same as -parenb -istrip cs8
   -pass8        same as parenb istrip cs7
   raw           same as -ignbrk -brkint -ignpar -parmrk -inpck -istrip
                 -inlcr -igncr -icrnl  -ixon  -ixoff  -iuclc  -ixany
                 -imaxbel -opost -isig -icanon -xcase min 1 time 0
   -raw          same as cooked
   sane          same as cread -ignbrk brkint -inlcr -igncr icrnl -iutf8
                 -ixoff -iuclc -ixany imaxbel 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, all special
                 characters to their default values.
Handle the tty line connected to standard input.  Without arguments,
prints baud rate, line discipline, and deviations from stty sane.  In
settings, CHAR is taken literally, or coded as in ^c, 0x37, 0177 or
127; special values ^- or undef used to disable special characters.
stty - 改变并打印终端行设置 
    帮助信息的中文译文
      用法
  stty [-F device] [--file=device] [SETTING]... 
  stty [-F device] [--file=device] [-a|--all] 
  stty [-F device] [--file=device] [-g|--save] 
  
      描述 
  打印或改变终端属性. 
  -a, --all
  以可读的格式打印当前的所有设置 
  -g, --save
  以终端可读的格式打印当前的所有设置 
  -F, --file
  打开指定的设备,并用此设备作为输入来代替标准输入 
  --help
  显示帮助并退出 
  --version
  显示版本信息并退出 
  
      可选项
      SETTING之前的负号'-'表示否定,星号*表明是非POSIX设置. 以下是一些系统定义了的可用设置: 
  
      特殊字符
  * dsusp CHAR 
      CHAR表示输入满时,发一个停止信号给终端 
  
      eof CHAR
  CHAR表示一个文件结束 (结束输入) 
  
      eol CHAR
  CHAR表示当前行结束 
  
      * eol2 CHAR 
      CHAR是另一个表示结束当前行的字符 
  
      erase CHAR
  CHAR表示擦除最后一个输入字符 
  
       intr CHAR
  CHAR表示发一个中断信号 
  
       kill CHAR
   CHAR表示擦除当前行 
  
       * lnext CHAR 
       CHAR表示输入下一个字符 
   
       quit CHAR
   CHAR表示发出一个退出信号 
  
        * rprnt CHAR
        CHAR表示刷新当前行 
   
         start CHAR
     CHAR表示在停止输出后重新开始输出 


       
         stop CHAR
     CHAR表示停止输出 
  
         susp CHAR
     CHAR表示发送一个终端停止信号 
     
         * swtch CHAR 
        CHAR表示切换到不同的外壳层 
  
         * werase CHAR 
         CHAR表示擦除已经输入的最后一个单词 
  
        特殊设置
   N
   把输入和输出的波特率设为N 
  
        * cols N 
        通知内核终端有N列 
  
        * columns N
        与cols N 相同 
  
        ispeed N
   设置输入速度为N 
  
         * line N 
        用行约束规则N 
  
        min N
    和-icanon一起用, 设置一次完整的读操作最小为N个字符 
   
        time N
   和-icanon一起用, 设置读超时为十分之N秒 
  
        ospeed N
   设置输出速度为N 
  
       * rows N 
       通知内核终端有N行 

  * size 
      根据内核打印出终端的行数和列数 
  
       speed
   打印出终端的速度 
  
       控制设置
  [-]clocal
  关闭解调器的控制信号 
  
      [-]cread
  允许接收输入 
  
      * [-]crtscts 
      允许 RTS/CTS 的握手 
  
      csN
  把字符长度设为N, N 为[5..8] 
  
      [-]cstopb
  对每字符使用两个停止位 (一个带有 `-') 
  
      [-]hup
  当最后一个进程关闭终端后,发一个挂起信号 
  
  [-]hupcl
  同 [-]hup 

  [-]parenb
  在输出中产生奇偶校验位,并要求在输入中也有奇偶校验位 

  [-]parodd
  设置奇校验 (偶校验用 `-') 


  输入设置:
  [-]brkint
  暂停并产生中断信号 
  
      [-]icrnl
  将回车解释为换行 
  
      [-]ignbrk
  忽略中断信号 

  [-]igncr
  忽略回车符 

  [-]ignpar
  忽略有奇偶校验错的字符 

  * [-]imaxbel 
      对一个字符产生嘟叫,但不刷新已满的输入缓冲区 


  [-]inlcr
  将换行解释为回车 

  [-]inpck
  打开输入奇偶校验 


  [-]istrip
  清除输入字符的高位(第8位) 

  * [-]iuclc 
      将大写字符转换成小写字符 

  * [-]ixany 
      使任何字符都重新开始输出(而 不仅仅是重新输出字符能实现此功能) 

  [-]ixoff
  打开发送开始/停止字符的开关 


  [-]ixon
  打开XON/XOFF的流量控制 


  [-]parmrk
  标记奇偶校验错误 (使用255-0-character 字符序列) 

  [-]tandem
  同 [-]ixoff 

  输出设置: 
  * bsN       回退延迟, N 为 [0..1] 

  * crN 回车延迟, N 为 [0..3] 

  * ffN 换页延迟, N 为 [0..1] 

  * nlN 换行延迟, N 为 [0..1] 

  * [-]ocrnl 将回车解释为换行 

  * [-]ofdel 使用删除字符来填充,而不是用空字符填充 

  * [-]ofill 使用填充字符,不使用定时延迟 

  * [-]olcuc 将小写字符转换成大写 

  * [-]onlcr 将换行解释为回车-换行 

  * [-]onlret 换行执行一次回车

  * [-]onocr 不在第一列打印回车 

  [-]opost  postprocess 输出 

  * tabN 水平tab键延迟, N 为 [0..3] 

  * tabs 同tab0 

  * -tabs 同tab3 

  * vtN 垂直方向tab键延迟。。。, N 为 [0..1] 

  本地设置:
  [-]crterase
  将擦除字符显示为:退格-空格-退格 

  * crtkill 根据echoprt和echoe的设置去除所有行 

  * -crtkill 根据echoctl和echok设置去除所有行 

  * [-]ctlecho 在头部符号中显示控制字符'^c') 

  [-]echo
  显示输入字符 

  * [-]echoctl 同 [-]ctlecho 

  [-]echoe
  同[-]crterase 

  [-]echok
  在一个杀死字符后显示一个换行 

  * [-]echoke 同 [-]crtkill 

  [-]echonl
  即使不显示其它字符也换行 

  * [-]echoprt 向后显示在 '' 和 '/'之间的擦除字符 

  [-]icanon
  打开 erase, kill, werase, 和 rprnt 这些特殊字符 

  [-]iexten
  打开 非POSIX 特殊字符 

  [-]isig
  打开中断,退出和挂起这些特殊字符 

  [-]noflsh
  在中断和退出这些特殊字符后禁止刷新 

  * [-]prterase 同 [-]echoprt 

  * [-]tostop 停止试图写终端的后台作业 

  * [-]xcase 与icanon同时使用, 表示用`'退出大写状态 

  综合设置: 
  * [-]LCASE 
      同 [-]lcase 

  cbreak
  同 -icanon 

  -cbreak
  同 icanon 

  cooked
  同 brkint ignpar istrip icrnl ixon opost isig icanon, eof 和 eol 字符被设为默认值 

  -cooked
  同 raw 

  crt
  同 echoe echoctl echoke 

  dec
  同 echoe echoctl echoke -ixany intr ^c erase 0177 kill ^u 

  * [-]decctlq
      同 [-]ixany 

  ek
  将擦除,杀死字符设为它们的默认值 

  evenp
  同 parenb -parodd cs7 

  -evenp
  同 -parenb cs8 

  * [-]lcase 同 xcase iuclc olcuc 

  litout
  同 fB-parenb -istrip -opost cs8 

  -litout
  同 parenb istrip opost cs7 

  nl
  同 -icrnl -onlcr 

  -nl
  同 icrnl -inlcr -igncr onlcr -ocrnl -onlret 

  oddp
  同 parenb parodd cs7 
  -oddp
  同 -parenb cs8 
  [-]parity
  同 [-]evenp 
  pass8
  同 -parenb -istrip cs8 
  -pass8
  同 parenb istrip cs7 
  raw
  同 -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuc lc -ixany -imaxbel -opost -
isig -icanon -xcase min 1 ti me 0 
  -raw
  同 cooked 

  sane
  同 cread -ignbrk brkint -inlcr -igncr icrnl -ixoff -iuclc -ixany imaxbel opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff 0 isig icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke, 所有特殊字符 使用缺省值. 

  本命令(stty)处理连接到标准输入的终端行.如不带参数,则输出波特率, 行的约束规则,以及与健全 的stty相背离的设置.在设置中, CHAR 是逐字读取的, 或是象 ^c, 0x37, 0177 或127这样的 编码; 特殊值 ^- 或未定义被用来禁止特殊字符.

整理自网络

0 0
原创粉丝点击