Read is an important Linux Command Used to read input from the keyboard or standard input.
Generally, the input is completed only when you press the Enter key. However, in some cases, you cannot press the Enter key. read provides a method that does not need to press the Enter key.
1.-p variable name of "prompt statement"
[Wang @ localhost desktop] $ vim testcmd. sh
#! /Bin/bash
Read-p "Enter your name:" name1 name2 // a space is required before name1. You can assign values to multiple variables.
Echo $ name1
Echo $ name2
[Wang @ localhost desktop] $ chmod + x testcmd. sh
[Wang @ localhost desktop] $./testcmd. sh
Enter your name: william sam
William
Sam
[Wang @ localhost desktop] $./testcmd. sh
Enter your name: william sam linux // redundant input will be assigned to the last variable
William
Sam linux
2.-n input count
When the number of characters entered reaches the predefined number, the system automatically exits without pressing enter.
[Wang @ localhost desktop] $ read-n 4-p "Enter your name:" name; echo $ name
Enter your name: wangwang // One is input, and the other is echo $ name.
3.-s does not show back
The password is used to protect the password.
[Wang @ localhost desktop] $ read-n 4-s-p "Enter your name:" name; echo $ name
Enter your name: wang // This is echo $ name
4.-t waiting for the input seconds
[Wang @ localhost desktop] $ read-n 4-t 2-p "Enter your name:" name; echo $ name
Enter your name: // automatically jumps out 2 seconds later
Or write a script:
[Wang @ localhost desktop] $ vim testcmd. sh
#! /Bin/bash
If read-t 2-p "Enter your name:" name
Then
Echo $ name
Else
Echo "Timeout! "
Fi
[Wang @ localhost desktop] $./testcmd. sh
Enter your name: Timeout!