Linux命令之stty

來源:互聯網
上載者:User

標籤:rom   尋找   logs   sqlplus   standard   樣本   用法   游標移動   使用者   

用途說明

stty命令用於顯示和修改終端行設定(change and print terminal line settings)。

常用參數

stty命令不帶參數可以列印終端行設定,加上-a參數可以列印得更詳細些。

stty size可以顯示終端的大小,即行數和列數。

stty命令還可以更改終端行的設定,格式如下:

stty SETTING CHAR

其中,SETTING可以是如下

    eof : 輸入結束,檔案結束,預設為Ctrl+D。比如:用cat >file來建立檔案時,按Ctrl+D來結束輸入。
    erase : 向後刪除字元,擦除最後一個輸入字元,預設為Ctrl+?。注意預設情況下退格鍵Backspace不是刪除字元。
    intr : 中斷當前程式,預設為Ctrl+C。
    kill : 刪除整條命令,刪除整行,預設為Ctrl+U。
    quit :退出當前程式,預設為Ctrl+\或Ctrl+|。
    start : 啟動螢幕輸出,預設為Ctrl+Q。
    stop :停止螢幕輸出,預設為Ctrl+S。
    susp : terminal stop當前程式,預設為Ctrl+Z。這樣當前進程就會變成後台進程了。

    werase:刪除最後一個單詞,預設為Ctrl+W。

stty命令還有一些其他用法,如:

    stty -echo 關閉回顯。比如在指令碼中用於輸入密碼時。

    stty echo 開啟回顯。


使用樣本樣本一 列印終端行設定

[[email protected] ~]# stty
speed 38400 baud; line = 0;
-brkint -imaxbel
[[email protected] ~]# stty -a
speed 38400 baud; rows 41; columns 132; 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 cs8 -hupcl -cstopb cread -clocal -crtscts -cdtrdsr
-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

[[email protected] ~]#

 

樣本二 列印當前終端的大小(行數和列數)

[[email protected] ~]# stty size
41 132

樣本三 設定退格鍵Backspace的刪除行為

在預設情況下,我們按退格鍵Backspace時,會在螢幕上回顯^H,而不是把前一個字元刪除。比如使用sftp/ftp/sqlplus/ij等命令時,就會碰到這種情況。我們可以使用stty命令把Backspace的行為變成刪除前一個字元。

[[email protected] ~]# sftp 192.168.6.12
Connecting to 192.168.6.12...
[email protected]‘s password:
sftp> get abc^H^H^H^H
Couldn‘t stat remote file: No such file or directory
File "/root" not found.
sftp> quit
[[email protected] ~]#
[[email protected] ~]# stty erase ^H
[[email protected] ~]# sftp 192.168.6.12
Connecting to 192.168.6.12...
[email protected]‘s password:
sftp> get abc
Couldn‘t stat remote file: No such file or directory
File "/root/abc" not found.
sftp> quit
[[email protected] ~]#

樣本四 在vi編輯檔案時按Ctrl+Q來結束終端僵死的局面

[[email protected] ~]# vi 1.txt
1
2
3
Ctrl+S
~

註:按了Ctrl+S之後,就會禁止螢幕輸出,從而出現終端僵死的情況。

這個時候,只要按Ctrl+Q就會結束這種局面,因為它會允許螢幕輸出。

Ctrl+Q

 

樣本五 在bash指令碼中與tty命令配合使用

在下面的指令碼中,先取得終端檔案名稱,以判斷指令碼是否運行在互動式方式還是在管道方式。

    #!/bin/sh                CONSOLE_INPUT=$(tty)                echo "CONSOLE_INPUT=$CONSOLE_INPUT"                COMMAND_LINE="java -Xmx640m -classpath .:lib/hyjc.jar:lib/log4j-1.2.15.jar:lib/commons-logging-1.1.1.jar:lib/proxool-0.9.0RC3.jar:lib/mysql-connector-java-5.1.10-bin.jar:lib/poi-3.5-ZHY-20091107.jar:lib/jdom-1.1.jar hyjc.sql.DBCLI MhrDB.properties MhrDB"                if [ "$CONSOLE_INPUT" == "not a tty" ]; then                $COMMAND_LINE        elif [ "$(whereis rlwrap)" == "rlwrap:" ]; then                stty erase ^H                $COMMAND_LINE        else                rlwrap -f jdb.rlwrap $COMMAND_LINE        fi

 

轉自:http://codingstandards.iteye.com/blog/826924


附:Linux終端常用快速鍵:

 

Ctrl + d       刪除一個字元,相當於通常的Delete鍵(命令列若無任何字元,則相當於exit;處理多行標準輸入時也表示EOF)

Ctrl + h       退格刪除一個字元,相當於通常的Backspace鍵

Ctrl + u       刪除游標之前到行首的字元

Ctrl + k       刪除游標之前到行尾的字元

Ctrl + c       取消當前行輸入的命令,相當於Ctrl + Break

Ctrl + a       游標移動到行首(Ahead of line),相當於通常的Home鍵

Ctrl + e       游標移動到行尾(End of line)

Ctrl + f       游標向前(Forward)移動一個字元位置

Ctrl + b       游標往回(Backward)移動一個字元位置

Ctrl + l       清屏,相當於執行clear命令

Ctrl + p       調出命令曆史中的前一條(Previous)命令,相當於通常的上箭頭

Ctrl + n       調出命令曆史中的下一條(Next)命令,相當於通常的上箭頭

Ctrl + r       顯示:號提示,根據使用者輸入尋找相關曆史命令(reverse-i-search)

Linux命令之stty

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.