{"id":134467,"date":"2019-01-27T08:03:47","date_gmt":"2019-01-27T00:03:47","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=134467"},"modified":"2019-01-23T13:07:05","modified_gmt":"2019-01-23T05:07:05","slug":"linux-shell-read","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/linux-shell-read.html","title":{"rendered":"Shell\u7f16\u7a0b\uff0cread\u7684\u7528\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u672c\u6587\u4ecb\u7ecdShell\u7f16\u7a0b\u4e2dread\u7684\u7528\u6cd5<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1\u3001read\u57fa\u672c\u8bfb\u53d6<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 #testing the read command\r\n  3 \r\n  4 echo -n \"Enter you name:\"   #echo -n \u8ba9\u7528\u6237\u76f4\u63a5\u5728\u540e\u9762\u8f93\u5165 \r\n  5 read name  #\u8f93\u5165\u7684\u591a\u4e2a\u6587\u672c\u5c06\u4fdd\u5b58\u5728\u4e00\u4e2a\u53d8\u91cf\u4e2d\r\n  6 echo \"Hello $name, welcome to my program.\"                                      \r\n\u6267\u884c\uff1a\r\n\r\n# .\/read.sh\r\nEnter you name: wangtao\r\nHello wangtao, welcome to my program.\r\n<\/pre>\n
2\u3001read -p \uff08\u76f4\u63a5\u5728read\u547d\u4ee4\u884c\u6307\u5b9a\u63d0\u793a\u7b26\uff09<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 #testing the read -p option\r\n  3 read -p \"Please enter your age: \" age\r\n  4 days=$[ $age * 365 ]\r\n  5 echo \"That makes you over $days days old!\"\r\n\u6267\u884c\uff1a\r\n\r\n# .\/age.sh\r\nPlease enter your age: 23\r\nThat makes you over 8395 days old!\r\n<\/pre>\n
3\u3001read -p \uff08\u6307\u5b9a\u591a\u4e2a\u53d8\u91cf\uff09<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # entering multiple variables\r\n  3 \r\n  4 read -p \"Enter your name:\" first last\r\n  5 echo \"Checking data for $last, $first\"\r\n\u6267\u884c\uff1a\r\n\r\n# .\/read1.sh\r\nEnter your name: a b\r\nChecking data for b, a\r\n<\/pre>\n
4\u3001read \u547d\u4ee4\u4e2d\u4e0d\u6307\u5b9a\u53d8\u91cf\uff0c\u90a3\u4e48read\u547d\u540d\u5c06\u5b83\u6536\u5230\u7684\u4efb\u4f55\u6570\u636e\u90fd\u653e\u5728\u7279\u6b8a\u73af\u5883\u53d8\u91cfREPLY\u4e2d<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # testing the REPLY environment variable\r\n  3 \r\n  4 read -p \"Enter a number: \"\r\n  5 factorial=1                         \r\n  6 for (( count=1; count< = $REPLY; count++ ))\r\n  7 do\r\n  8    factorial=$[ $factorial * $count ]   #\u7b49\u53f7\u4e24\u7aef\u4e0d\u8981\u6709\u7a7a\u683c\r\n  9 done\r\n 10 echo \"The factorial of $REPLY is $factorial\"\r\n\u6267\u884c\uff1a\r\n\r\n.\/read2.sh\r\nEnter a number: 6\r\nThe factorial of 6 is 720\r\n<\/pre>\n
5\u3001\u8d85\u65f6, \u7b49\u5f85\u8f93\u5165\u7684\u79d2\u6570\uff08read -t\uff09<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # timing the data entry\r\n  3 \r\n  4 if read -t 5 -p \"Please enter your name: \" name     #\u8bb0\u5f97\u52a0-p\u53c2\u6570\uff0c \u76f4\u63a5\u5728read\u547d\u4ee4\u884c\u6307\u5b9a\u63d0\u793a\u7b26\r\n  5 then\r\n  6     echo \"Hello $name, welcome to my script\"\r\n  7 else\r\n  8     echo \r\n  9     echo \"Sorry, too slow!\"\r\n 10 fi\r\n\u6267\u884c\uff1a\r\n\r\n# .\/read3.sh\r\nPlease enter your name: \r\nSorry, too slow!\r\n# .\/read3.sh \r\nPlease enter your name: wang\r\nHello wang, welcome to my script\r\n<\/pre>\n
6\u3001read\u547d\u4ee4\u5bf9\u8f93\u5165\u7684\u5b57\u7b26\u5224\u65ad<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # getting just one character of input\r\n  3 \r\n  4 read -n1 -p \"Do you want to continue [Y\/N]? \" answer\r\n  5 case $answer in\r\n  6 Y | y) echo\r\n  7        echo \"fine, continue on...\";;\r\n  8 N | n) echo \r\n  9        echo \"OK, goodbye\"\r\n 10        exit;;\r\n 11 esac   \r\n\u6267\u884c\uff1a\r\n\r\n# .\/read4.sh\r\nDo you want to continue [Y\/N]? y\r\nfine, continue on...\r\n\r\n.\/read4.sh\r\nDo you want to continue [Y\/N]? n\r\nOK, goodbye\r\n<\/pre>\n
7\u3001\u9690\u85cf\u65b9\u5f0f\u8bfb\u53d6\uff08read -s\uff09<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # hiding input data from the monitor\r\n  3 \r\n  4 read -s -p \"Enter your passwd: \" pass   #-s \u53c2\u6570\u4f7f\u5f97read\u8bfb\u5165\u7684\u5b57\u7b26\u9690\u85cf\r\n  5 echo \r\n  6 echo \"Is your passwd readlly $pass?\"\r\n~                                          \r\n\u6267\u884c\uff1a\r\n\r\n# .\/read5.sh\r\nEnter your passwd: \r\nIs your passwd readlly osfile@206?\r\n<\/pre>\n
8\u3001\u4ece\u6587\u672c\u4e2d\u8bfb\u53d6<\/strong><\/div>\n
\r\n  1 #!\/bin\/bash\r\n  2 # reading data from a file\r\n  3 \r\n  4 count=1\r\n  5 cat test | while read line\r\n  6 do\r\n  7    echo \"Line $count: $line\"\r\n  8    count=$[ $count + 1 ]\r\n  9 done\r\n 10 echo \"Finished processing the file\"\r\n\u6267\u884c\uff1a\r\n\r\n .\/read6.sh\r\nLine 1: The quick brown dog jumps over the lazy fox.\r\nLine 2: This is a test, this is only a test.\r\nLine 3: O Romeo, Romeo! Wherefore art thou Romeo?\r\nFinished processing the file\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

1 #!\/bin\/bash 2 #testing the read command 3 4 echo -n ” […]<\/p>\n","protected":false},"author":63,"featured_media":134468,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-134467","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/134467","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/63"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=134467"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/134467\/revisions"}],"predecessor-version":[{"id":134771,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/134467\/revisions\/134771"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/134468"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=134467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=134467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=134467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}