stty 是一個用來改變並列印終端行設置的常用命令。
1、在命令列下禁止輸出大寫的方法。相關設置:
stty -icanon # 設置一次性讀完操作,如 getchar() 不需按 enter stty icanon # 取消上面設置 stty -a # 查看資訊 stty -echo # 關閉命令回顯,鍵入的任何字元均不在螢幕顯示 stty echo # 取消不顯示設置 stty raw # 終端驅動處於一次一個字元模式 stty cooked # 終端驅動回到一次一行模式 stty iuclc # 開啟 stty -iuclc # 恢復
Linux 中顯示密碼為 * 的範例:
int main() {
char c[7];
int i;
system("stty -icanon");
system("stty -echo");
for(i = 0; i < 6; i++) {
c[i] = getchar();
printf("*");
}
c[i] = '\0';
system("stty echo");
return 0;
}2、在命令列下禁止輸出小寫:
stty olcuc # 開啟 stty -olcuc # 恢復
3、列印終端的行數和列數:stty size
4、改變 ctrl+D 的方法:stty eof "string",系統預設用 ctrl+D 表示檔案結束,透過此方法可以改變。
5、屏蔽顯示:stty -echo(禁止回顯)、stty echo(打開回顯)。測試方法:stty -echo;read;stty echo;read
6、忽略回車符:stty igncr(開啟)、stty -igncr(恢復)。
7、定時輸入:
timeout_read() {
timeout=$1
old_stty_settings=`stty -g` # save current settings
stty -icanon min 0 time 100 # set 10seconds
eval read varname
stty "$old_stty_settings" # recover settings
}更簡單的方法是利用 read 命令的 -t 選項:read -t 10 varname。