[ 永遠的UNIX::UNIX技術資料的寶庫 ]   GB | BIG5

首頁 > 編程技術 > Shell > 正文
寫 shell script 時,要如何從 terminal 讀入字元?
Steve Hayman (2001-04-19 10:28:46)
在 sh 中,你可以用 read。通常是使用在回圈,如下例: 

        while read line 
        do 
            ... 
        done 

在 csh 中,則用 $<: 

        while ( 1 ) 
            set line = "$<" 
            if ( "$line" == "" ) break 
                ... 
        end 

很可惜的,csh 並沒有方法判斷空白行和檔案結尾(end-of-file)的不同。 

如果你要用 sh 從 terminal 讀一個字元,那你可以試試 

        echo -n "Enter a character: " 
        stty cbreak         # or  stty raw 
        readchar=`dd if=/dev/tty bs=1 count=1 2>/dev/null` 
        stty -cbreak 
        echo "Thank you for typing a $readchar ."    (http://www.fanqiang.com)
    進入【UNIX論壇

相關文章

======
 

★  樊強制作 歡迎分享  ★