Basic usage
? ? The read command is primarily used to read from or read from a standard input, and to save the information to a variable. Its common usage is as follows:
read [选项] [文件]
Options |
explain |
-A array |
Reads content into numeric values, variable defaults to array and spaces as delimiters |
-D delimiter |
Stop reading if the specified character is encountered |
-N Nchars |
Specifies the maximum number of characters that can be read, which defines the length of the input text |
-R |
Mask Escape character |
-P Prompt |
Display the prompt information |
-S |
Silent mode, not displayed in the terminal when entering characters, often used for password input, etc. |
-T Timeout |
Specify the time-out period |
-U FD |
Read in from the file descriptor, the FD can be opened by exec |
Usage examples
1. Read in from standard input
[[email protected] test]# cat read.sh#!/bin/bashecho -n "Please input your name:"read nameecho "Hello $name"[[email protected] test]# bash read.shPlease input your name:JackHello Jack
2, specify the display information from the standard input read into
[[email protected] test]# cat read.sh#!/bin/bash# echo -n "Please input your name:"read -p "Please input your name:" name# read nameecho "Hello $name"[[email protected] test]# bash read.shPlease input your name:JackHello Jack
In the example above, read can accept more than one parameter at a time, as follows:
read -p "Please input your name:" firstName secondName lastName
However, the following matters need to be noted:
- If the input data is less than the number of variables, the extra variable is not fetched to the data, that is, the variable value is empty
- If you enter more data than the number of variables, the excess data will all be assigned to the last variable
- If no variables are defined after the Read command, when the script executes, if the user enters data, the data is saved to the environment variable $REPLY
3. Specify the time-out period
[[email protected] test]# cat read.sh#!/bin/bashif read -t 3 -p "Please input your name:" firstName secondName lastNamethen echo "variable is $firstName - $secondName - $lastName"else echo -e "\ntimeout\n"fi[[email protected] test]# bash read.shPlease input your name:timeout
4. Read content from the specified file
[[email protected] test]# cat-n test.txt 1 This is Test text. 2 this was second line. 3 Hello World 4 C # Main 5 python# using the-u option [[email protected] test]# cat readtest.sh#!/bin/bashexec 5< ~/t Est/test.txtcount=0while read-u 5 vardo let count= $count +1 echo "line $count are $var" Doneecho "Total line count is $count "Exec 5<&-[[email protected] test]# bash Readtest.shline 1 is this is Test text. Line 2 is the second line. Line 3 are Hello WorldLine 4 is C # MainLine 5 is Pythontotal lines count is AA use piping [[email protected] test]# cat Readte St.sh#!/bin/bashcount=1cat ~/test/test.txt | While read Linedo echo ' current line $count-$line ' let count= $count +1doneecho ' total line count is $count ' [[email Protected] test]# Bash Readtest.shcurrent line 1-this is Test text. Current line 2-this are second line. 3-hello worldcurrent line 4-c# maincurrent line 5-pythontotal line count is # Use REDIRECT [[Email protec Ted] test]# Cat Readtest.sh#!/bin/bashcount=0while Read Linedo let count= $count +1 Echo ' current line $count-$line ' Done < ~/test /test.txtecho "Total line count was $count" [[email protected] test]# bash readtest.shcurrent line 1-this is test Tex T.current line 2-this are second line. 3-hello worldcurrent line 4-c# maincurrent line 5-pythontotal line count is 5
This article is posted on the subscription number, such as your friends like my article, you can also follow my subscription number: Woaitest, or scan the following QR code to add attention:
Linux Basic Tutorial 45-read command