Get the App
SLTechnology News&Howtos  ›  Servers  › 

What are the common parameters of the read command in shell

Shulou Source: shulou.com Published: 2022-06-01 14:02:59 09月24日 Update

Editor to share with you what are the common parameters of the read command in shell, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

We know that there are two ways to receive incoming parameters in Shell. One is to pass parameters through a script, and the other is to receive incoming parameters through read. A simple example of passing parameters through a script is as follows:

# passed by script, where $0 refers to the script name, $1 is the first parameter, and $2 is the second parameter [root@host ~] #. / script.sh 1 2Total = 3 [root@host ~] # vim script function add () {total=$ (expr $1 + $2) echo-e "Total = $total"} add $1 $2

To receive the incoming parameters through read, let's first look at the basic format of read:

Read [- rs] [- an ARRAY] [- d delim] [- n nchars] [- N nchars] [- p prompt] [- t timeout] [var1 var2 var3.]

[root@host ~] # / script.sh Enter Password:The password your input is: Test@1234\ [root@host ~] # vim script.shemaking bind Bash read-N10-T30-r-s-d $- p "Enter Password:" passwordecho-e "\ nThe password your input is:$password"

-p prompt statement, followed by input prompt information, here is' Enter Password:'

The number of-n parameters, sometimes it is necessary to limit the password length, or other input length restrictions, such as [Ymax N], enter only one digit,-N1

-s screen echo, the input is not displayed on the screen, and is generally used for password input

-t wait time. Set here for 30 seconds. If it is not entered within 30 seconds or the input is incomplete, it will be terminated.

-d input limit, here is $, input to $, natural termination of input

-r masks the translation function of the special character\, and then treats it as an ordinary character.

From the above example, it basically includes most of the commonly used functions above, especially the parameters such as-pmam talk nm m et al., you can learn the command read very well.

These are all the contents of the article "what are the common parameters of the read command in shell?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Parameters input scripts commands commonly used content articles functions most characters passwords length learning prompts restrictions general special not so many number examples Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Shulou Technology Huawei macOS Docker