您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“shell read命令怎么用”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
shell作為一門語言,自然也具有讀數(shù)據(jù)的功能,read就是按行從文件(或標準輸入或給定文件描述符)中讀取數(shù)據(jù)的最佳選擇。當使用管道、重定向方式組合命令時感覺達不到自己的需求時,不妨考慮下while read line。
1 #!/bin/bash 2 #testing the read command 3 4 echo -n "Enter you name:" #echo -n 讓用戶直接在后面輸入 5 read name #輸入的多個文本將保存在一個變量中 6 echo "Hello $name, welcome to my program." 執(zhí)行:# ./read.shEnter you name: wangtao Hello wangtao, welcome to my program.
1 #!/bin/bash 2 #testing the read -p option 3 read -p "Please enter your age: " age 4 days=$[ $age * 365 ] 5 echo "That makes you over $days days old!"執(zhí)行:# ./age.shPlease enter your age: 23 That makes you over 8395 days old!
1 #!/bin/bash 2 # entering multiple variables 3 4 read -p "Enter your name:" first last 5 echo "Checking data for $last, $first"執(zhí)行:# ./read1.shEnter your name: a b Checking data for b, a
1 #!/bin/bash 2 # testing the REPLY environment variable 3 4 read -p "Enter a number: " 5 factorial=1 6 for (( count=1; count$REPLY; count++ )) 7 do 8 factorial=$[ $factorial * $count ] #等號兩端不要有空格 9 done10 echo "The factorial of $REPLY is $factorial"執(zhí)行: ./read2.sh Enter a number: 6 The factorial of 6 is 720
1 #!/bin/bash 2 # timing the data entry 3 4 if read -t 5 -p "Please enter your name: " name #記得加-p參數(shù), 直接在read命令行指定提示符 5 then 6 echo "Hello $name, welcome to my script" 7 else 8 echo 9 echo "Sorry, too slow!"10 fi執(zhí)行:# ./read3.shPlease enter your name: Sorry, too slow!# ./read3.shPlease enter your name: wang Hello wang, welcome to my script
1 #!/bin/bash 2 # getting just one character of input 3 4 read -n1 -p "Do you want to continue [Y/N]? " answer 5 case $answer in 6 Y | y) echo 7 echo "fine, continue on...";; 8 N | n) echo 9 echo "OK, goodbye"10 exit;; 11 esac 執(zhí)行:# ./read4.shDo you want to continue [Y/N]? y fine, continue on... ./read4.sh Do you want to continue [Y/N]? n OK, goodbye
1 #!/bin/bash 2 # hiding input data from the monitor 3 4 read -s -p "Enter your passwd: " pass #-s 參數(shù)使得read讀入的字符隱藏 5 echo 6 echo "Is your passwd readlly $pass?"~ 執(zhí)行:# ./read5.shEnter your passwd: Is your passwd readlly osfile@206?
1 #!/bin/bash 2 # reading data from a file 3 4 count=1 5 cat test | while read line 6 do 7 echo "Line $count: $line" 8 count=$[ $count + 1 ] 9 done10 echo "Finished processing the file"執(zhí)行: ./read6.sh Line 1: The quick brown dog jumps over the lazy fox. Line 2: This is a test, this is only a test. Line 3: O Romeo, Romeo! Wherefore art thou Romeo? Finished processing the file
“shell read命令怎么用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。