新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
詳解Shellread
要與Linux交互,腳本獲取鍵盤輸入的結(jié)果是必不可少的,read可以讀取鍵盤輸入的字符。

我們擁有十年網(wǎng)頁設(shè)計(jì)和網(wǎng)站建設(shè)經(jīng)驗(yàn),從網(wǎng)站策劃到網(wǎng)站制作,我們的網(wǎng)頁設(shè)計(jì)師為您提供的解決方案。為企業(yè)提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、微信開發(fā)、小程序制作、手機(jī)網(wǎng)站制作設(shè)計(jì)、H5響應(yīng)式網(wǎng)站、等業(yè)務(wù)。無論您有什么樣的網(wǎng)站設(shè)計(jì)或者設(shè)計(jì)方案要求,我們都將富于創(chuàng)造性的提供專業(yè)設(shè)計(jì)服務(wù)并滿足您的需求。
shell作為一門語言,自然也具有讀數(shù)據(jù)的功能,read就是按行從文件(或標(biāo)準(zhǔn)輸入或給定文件描述符)中讀取數(shù)據(jù)的最佳選擇。當(dāng)使用管道、重定向方式組合命令時(shí)感覺達(dá)不到自己的需求時(shí),不妨考慮下while read line。
1、read基本讀取
1 #!/bin/bash
2 #testing the read command
3
4 echo -n "Enter you name:" #echo -n 讓用戶直接在后面輸入
5 read name #輸入的多個(gè)文本將保存在一個(gè)變量中
6 echo "Hello $name, welcome to my program."
執(zhí)行:
# ./read.sh
Enter you name: wangtao
Hello wangtao, welcome to my program.
2、read -p (直接在read命令行指定提示符)
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.sh
Please enter your age: 23
That makes you over 8395 days old!
3、read -p (指定多個(gè)變量)
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.sh
Enter your name: a b
Checking data for b, a
4、read 命令中不指定變量,那么read命名將它收到的任何數(shù)據(jù)都放在特殊環(huán)境變量REPLY中
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 done
10 echo "The factorial of $REPLY is $factorial"
執(zhí)行:
./read2.sh
Enter a number: 6
The factorial of 6 is 720
5、超時(shí), 等待輸入的秒數(shù)(read -t)
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.sh
Please enter your name:
Sorry, too slow!
# ./read3.sh
Please enter your name: wang
Hello wang, welcome to my script
6、read命令對輸入的字符判斷
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.sh
Do you want to continue [Y/N]? y
fine, continue on...
./read4.sh
Do you want to continue [Y/N]? n
OK, goodbye
7、隱藏方式讀?。╮ead -s)
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.sh
Enter your passwd:
Is your passwd readlly osfile@206?
8、從文本中讀取
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 done
10 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
網(wǎng)頁標(biāo)題:詳解Shellread
網(wǎng)站URL:http://m.5511xx.com/article/codjeec.html


咨詢
建站咨詢
