linux命令之互動式輸入read read是一個重要的命令,用於從鍵盤或標準輸入中讀取輸入。一般只有按斷行符號鍵的時候才標誌輸入完畢,但有些情況下沒法按斷行符號鍵,read提供了一種不需要按斷行符號鍵的方法。1.-p “提示句” 變數名[wang@localhost 案頭]$ vim testcmd.sh#!/bin/bashread -p "Enter your name :" name1 name2 //name1前面要空格,可以賦值給多個變數echo $name1echo $name2 [wang@localhost 案頭]$ chmod +x testcmd.sh [wang@localhost 案頭]$ ./testcmd.sh Enter your name :william samwilliamsam[wang@localhost 案頭]$ ./testcmd.sh Enter your name :william sam linux //多餘的輸入會賦值給最後一個變數williamsam linux 2.-n 輸入個數當輸入字元數達到預定數目,則自動結束,不用按斷行符號。[wang@localhost 案頭]$ read -n 4 -p "Enter your name :" name;echo $nameEnter your name :wangwang //一個wang是輸入的,另一個是echo $name。 3.-s 不回顯用於輸入密碼,對於密碼的保護。[wang@localhost 案頭]$ read -n 4 -s -p "Enter your name :" name;echo $nameEnter your name :wang //這個是echo $name 4.-t 等待輸入的秒數[wang@localhost 案頭]$ read -n 4 -t 2 -p "Enter your name :" name;echo $nameEnter your name : //等2秒後 自動跳出了 或者寫個指令碼:[wang@localhost 案頭]$ vim testcmd.sh#!/bin/bashif read -t 2 -p "Enter your name :" namethen echo $nameelse echo "Timeout!"fi[wang@localhost 案頭]$ ./testcmd.sh Enter your name :Timeout! 5.-d 自訂定界符輸入自訂的定界符,結束輸入。[wang@localhost 案頭]$ read -d ":" -p "Enter your name :" name;echo $nameEnter your name :name:name 6.從標準輸入中讀取[wang@localhost 案頭]$ vim testcmd.sh#!/bin/bashcount=1cat test.c | while read line //從test.c讀取每一行並賦值給line變數do echo "$count:$line" count=$[ count+1 ]done [wang@localhost 案頭]$ cat test.c#include <stdio.h> int main(){int a = 4;(++a) += a;printf("we are the best %d!\n",a);return 0;} [wang@localhost 案頭]$ ./testcmd.sh 1:#include <stdio.h>2:3:int main()4:{5:int a = 4;6:(++a) += a;7:printf("we are the best %d!n",a);8:return 0;9:}注意:上面的cat test.c 也可以換成ls ,ps,grep,find等等命令