Shell-6.shell Read command
Read--Reads the information from the keyboard or file and assigns it to a variable
1. Read variable name
Instance
[email protected] home]# cat name.sh
#!/bin/sh
Echo-n "Please input your name:"
Read name
echo "Name is ${name}"
Show Results:
[Email protected] home]# sh name.sh
Please input your Name:yuanji
Name is Yuanji
Multiple variables can be read at one time
Read variable name 1 variable Name 2 ...
[email protected] home]# cat info.sh
#!/bin/sh
Read-p "Please input your name,age:" The name Age
echo "Name is ${name}. Is $age "
Show Results:
[Email protected] home]# sh info.sh
Please input your Name,age:yuanji 30
Name is Yuanji. Age is 30
2.-P parameter
Use Echon-n "..." above Give a prompt,
can be used
Read-p "What to display" command name
Instance
[email protected] home]# cat name.sh
#!/bin/sh
Read-p "Please input your name:" Name
echo "Name is ${name}"
Show Results:
[Email protected] home]# sh name.sh
Please input your Name:tom
Name is Tom
number of seconds to wait after the 3.-T parameter
Indicates the time, in seconds, to wait for input,
Wait time is over, the subsequent script will continue to execute,
Instance
[email protected] home]# cat name.sh
#!/bin/sh
Read-p "Please input your name:"-T 5 name
echo "Name is ${name}"
The 4.-s parameter sometimes requires script user input, but does not want the input data to be displayed. such as the user password.
[email protected] home]# cat passwd.sh
#!/bin/sh
Echo-n "Please Input your password:"
Read-s passwd
Echo-e "\N\N$PASSWD"
Show Results:
[Email protected] home]# sh passwd.sh
Please Input your password:
Abc
5.-n Parameters
Specifies the number of characters to be received by variables after read
Instance
[email protected] home]# cat name.sh
#!/bin/sh
Read-n 6-p "Please input your name:" Name
echo ""
echo "Name is ${name}."
6.-a Parameters
Assign to array variable "about array follow-up description"
Instance
[email protected] home]# cat aa.sh
#!/bin/sh
Echo-n "Input muliple values into an array:"
Read-a Array
echo "Get ${#array [@]} values in array"
echo "==========="
Echo ${array[*]}
Show Results:
[Email protected] home]# sh aa.sh
Input muliple values into an array:abc hello Yuanji
Get 3 values in array
===========
ABC Hello Yuanji
The following sections are described in follow-up content
Read reading files
About array Follow-up instructions
The above is a personal point of summary and understanding, the level is not high, the level of writing is very bad, please greatly forgive me.
Can exchange learning together.
My qq:610851588.
Can join my build group (now very few people, hope slowly more up)
Linux Clusters: 183932302
Python, Shell AC Group: 469094734
This article from the "Go to the Origin dimension" blog, reproduced please contact the author!
Shell-6.shell Read command