linux命令之互動式輸入read

來源:互聯網
上載者:User

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等等命令 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.