Interactive input of Linux commands read

Source: Internet
Author: User

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!

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.