A command that lets you understand the common arguments of the shell command

  • 2021-01-03 21:15:24
  • OfStack

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 a parameter through a script is as follows:


#  It's passed through the script, here $0 Refers to the script name ,$1 For the first 1 A parameter ,$2 For the first 2 A parameter 
[root@host ~]# ./script.sh 1 2
Total = 3
[root@host ~]# vim script.sh
#!/bin/bash
function add() {
  total=$(expr $1 + $2)
  echo -e "Total = $total"
}
add $1 $2

To receive incoming parameters through read, consider the basic format of read:

[

read [-rs] [-a 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.sh
#!/bin/bash
read -n10 -t30 -r -s -d $ -p "Enter Password:" password
echo -e "\nThe password your input is:$password"
-ES15en prompt statement, followed by input prompt message, here 'Enter Password: ' -ES18en number of arguments, sometimes to limit password length, or other input length limits, such as [Y/N], where only one bit is entered,-n1 -s block echo, no display of input content on the screen, 1 generally used for password input -ES23en wait time, here set 30 seconds, no input or incomplete input within 30 seconds, terminate -ES24en input threshold, here $, input to $, natural termination of input -r blocks the translation of special characters \, which are then treated as normal characters

From the above 1 example, basically includes most of the common functions above, especially -ES27en, -ES28en, -ES29en, -ES30en and other parameters, you can learn the read command very well

conclusion


Related articles: