In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 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 Linux read command". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
The read command is commonly used in Linux to read numeric values from standard input, and the read internal command is used to read single lines of data from standard input. This command can be used to read keyboard input, and when redirection is used, to read a line of data from a file.
phrasing read [-ers] [-a aname] [-d delim] [-i text] [-n nchars] [-N nchars] [-p prompt] [-t timeout] [-u fd] [name ...] Parameter description:
-a is followed by a variable, which is considered an array and assigned a value, by default separated by a space.
-d is followed by an identifier, but only the first character after it is useful as an end marker.
-p is followed by a prompt, which prints the prompt before typing.
-e can be typed using command completion.
-n followed by a number, defines the length of the input text, very useful.
-r mask\, if not this option, then\as an escape character, if any\is a normal character.
-s Quiet mode, no longer displayed on the screen when entering characters, such as login when entering a password.
-t followed by the number of seconds, defines the waiting time for the input character.
-u followed by fd, read from the file descriptor, which can be exec newly opened.
Example 1: Simple reading
#!/ bin/bash #This defaults to newline echo "Enter website name: " #Read input from keyboard read website echo "The website name you entered is $website" exit 0 #exit test results are:
Enter website name: www.runoob.com The website name you entered is www.runoob.com2, the-p parameter allows you to specify a prompt directly on the read command line.
#!/ bin/bash read -p "Enter website name:" website echo "The website name you entered is $website" exit 0 Test result is:
Enter website name: www.runoob.com The website name you entered is www.runoob.com3. The-t parameter specifies the number of seconds the read command waits for input. When the timer expires, the read command returns a non-zero exit status.
#!/ bin/bash if read -t 5 -p "Enter website name:" website then echo "The website name you entered is $website" else echo "\n Sorry, your input timed out. " fi exit 0 execute program no input, wait 5 seconds later:
Enter the website name: Sorry, you entered a timeout 4, in addition to entering the time, you can also use the-n parameter to set the read command to count the characters entered. When the number of input characters reaches a predetermined number, it automatically exits and assigns the input data to variables.
#!/ bin/bash read -n1 -p "Do you want to continue [Y/N]? " answer case $answer in Y | y) echo "fine ,continue";; N | n) echo "ok,good bye";; *) echo "error choice";; esac exit 0 This example uses the-n option followed by the value 1, indicating that the read command exits as soon as it receives one character. As soon as you press a character to answer, the read command accepts input and passes it to the variable immediately, without pressing the Enter key.
Exit after receiving only 2 inputs:
#!/ bin/bash read -n2 -p "Please enter two characters at random: " any echo "\nThe two characters you entered are:$any" exit 0 Execute the program and enter two characters:
Please enter two characters at random: 12 The two characters you enter are:125. The-s option prevents the data entered in the read command from being displayed on the command terminal (in fact, the data is displayed, except that the read command sets the text color to the same color as the background). Enter your password. Use this option.
#!/ bin/bash read -s -p "Please enter your password:" pass echo "\nThe password you entered is $pass" exit 0 Executing the program After entering the password, it is not displayed:
Please enter your password: The password you entered is runoob6. Read File
Each invocation of the read command reads "one line" of text from the file. When the file has no readable lines, the read command exits with a non-zero state.
How do I transfer data from a file to a read? Use the cat command and pipe the result directly to the while command containing the read command.
The test file test.txt reads as follows:
123 456 runoob test code:
#!/ bin/bash count=1 #assignment statement without spaces cat test.txt| while read line The output of the # cat command is used as input to the read command, and the values read to> are placed in the line do echo "Line $count:$line" count=$[ $count + 1 ] #Note the spaces in brackets. done echo "finish" exit 0 The result is:
Line 1:123 Line 2:456 Line 3:runoob finish Using the-e parameter, the following example enters the character a and presses the Tab key to output the relevant file name (the directory exists):
$ read -e -p "Enter filename:" str Enter filename:a a.out a.py a.pyc abc.txt Enter the file name:a"Linux read command how to use" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!
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.