In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "introduction and use of Linux read commands". 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!
I. Overview
The read command receives input from standard input (keyboard) or other file descriptors. After getting the input, the read command puts the data into a standard variable.
Second, use examples (only some commonly used options are listed here)
1. Basic reading
The code is as follows:
#! / bin/bash
Echo-n "Enter your name:" the function of parameter-n is no line wrapping, and echo is newline by default
Read name # enter from keyboard
Echo "hello $name, welcome to my program"
Exit 0 # exits the shell program.
It is equivalent to the following:
The code is as follows:
Read-p "Enter your name:" name #-p parameter, which allows you to specify a prompt directly on the read command line
There is only one variable after the above read, or there can be more than one. If you enter multiple data, the first data is given to the first variable and the second data is given to the second variable. If there are too many input data, all the remaining values will be given to the last variable, and if there is too little input, it will not end.
two。 Variables can also be left unspecified on the read command line
If you do not specify a variable, the read command places the received data in the environment variable REPLY
The code is as follows:
Read-p "Enter a number"
Echo $REPLY
3. Timing input
There are potential dangers in using the read command. The script will most likely stop and wait for input from the user. If the script must continue regardless of whether or not you enter data, you can use the-t option to specify a timer that specifies the number of seconds the read command waits for input. When the time is full, the read command returns a non-zero value (0 is the normal exit state)
The code is as follows:
#! / bin/bash
Ifread-t 5-p "please enter your name:" name
Then
Echo "hello $name, welcome to my script"
Else
Echo "sorry,too slow"
Fi
Exit 0
3. Set the characters entered by the count
When the number of characters entered reaches a predetermined number, it automatically exits and assigns the input data to the variable.
The code is as follows:
#! / bin/bash
Read-N1-p "Do you want to continue [Y _ bat N]?" answer
Case $answerin
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 a value of 1, to instruct the read command to exit as soon as it receives one character. As long as you press one character to answer, the read command immediately accepts the input and passes it to the variable. There is no need to press enter.
4. Read silently (input is not displayed on the monitor)
Sometimes you don't want the input data to be displayed on the monitor. A typical example is entering passwords, and of course there are many other data that need to be hidden. The-s option prevents the data entered in the read command from being displayed on the monitor (in fact, the data is displayed, except that the read command sets the text color to the same color as the background).
The code is as follows:
#! / bin/bash
Read-s-p "Enter your password:" pass
Echo "your password is $pass"
Exit 0
5. Read the file
The "one line" text in the file is read each time the read command is called. When the file has no readable lines, the read command exits in a non-zero state. The key to reading a file is how to transfer the data in the text to the read command. The most common method is to use the cat command on the file and pipe the results directly to the while command that contains the read command.
The code is as follows:
#! / bin/bash
Count=1
Cat dat | the output of the whileread line # cat command is used as the input of the read command, and the values read by read are put in the line
Do
Echo "$count:$line"
Count=$ (($count+ 1))
Done
Exit 0
This is the end of the introduction and usage of Linux 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.