In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use the read command of shell". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Read basically reads 1 #! / bin/bash 2 # testing the read command 3 4 echo-n "Enter you name:" # echo-n lets the user enter 5 read name # text directly behind it. The multiple text entered will be saved in a variable 6 echo "Hello $name, welcome to my program." Execute: #. / read.shEnter you name: wangtaoHello wangtao, welcome to my program.2, read-p (specify the prompt directly on the read command line) 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!" Execute: #. / age.shPlease enter your age: 23That makes you over 8395 days oldholders 3, read-p (specify multiple variables) 1 #! / bin/bash 2 # entering multiple variables 3 4 read-p "Enter your name:" first last 5 echo "Checking data for $last, $first" execute: #. / read1.shEnter your name: a bChecking data for b, A4, read commands do not specify variables Then the read name puts any data it receives in the special environment variable 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] # No spaces at both ends of the equal sign 9 done10 echo "The factorial of $REPLY is $factorial" execute:. / read2.shEnter a number: 6The factorial of 6 is 7205, timeout, number of seconds to wait for input (read-t) 1 #! / bin/bash 2 # timing the data entry 3 4 if read-t 5-p "Please enter your name:" name # remember to add-p parameter Directly on the read command line, specify the prompt 5 then 6 echo "Hello $name, welcome to my script" 7 else 8 echo 9 echo "Sorry, too slow!" 10 fi execution: #. / read3.shPlease enter your name:Sorry, too characters #. / read3.shPlease enter your name: wangHello wang, welcome to my script6, read command to judge the entered characters 1 #! / bin/bash 2 # getting just one character of input 34 read-N1-p "Do you want to continue [Yhand 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 execution: #. / read4.shDo you want to continue [Yamei N]? Yfine, continue on..../read4.shDo you want to continue [Y/N]? NOK Goodbye7, hidden read (read-s) 1 #! / bin/bash 2 # hiding input data from the monitor 3 4 read-s-p "Enter your passwd:" pass #-s parameters make the characters read by read hide 5 echo 6 echo "Is your passwd readlly $pass?" ~ execute: #. / read5.shEnter your passwd:Is your passwd readlly osfile@206?8, Read 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" execute:. / read6.shLine 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! This is the end of Wherefore art thou Romeo?Finished processing the file's "how to use shell's read commands". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.