寫shellscript時,要如何從terminal讀入字元?

 
在 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.5h6.com/article/10118.html
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章