[Shell Foundation]--read Command

Source: Internet
Author: User

Read command: Primarily used in the shell to read input, variables, text

1. accept input from the standard input (keyboard) and assign the input data to the SET variable
"Press Enter--indicates that the input is complete"
"If more data is entered than the number of variables set, the extra portion is assigned to the last variable"
"If the variable is not set, the input data is assigned to the environment variable replay"

#!/bin/bashecho"Enter your name:"read name1 name2Echo  Hello, $name 1, $name. /read. SH Enter your Name:taeyeon jessicahello,taeyeon,jessica

2.- p print a prompt directly in the Read command line

#!/bin/bashread  -P  "Enter your name:"  name1  name2  echo  Hello, $name 1, $name. /read. SH Enter your Name:taeyeon jessicahello,taeyeon,jessica

3.-T implements timing input. Specifies the number of seconds that the read command waits for input.

#!/bin/BashifRead-t5-P"Enter your name:"Name # #-after p to direct the prompt, pay attention to multi-option how to use Then   EchoHello, $nameElse   Echo-E"\nsorry,too Slow"fiExit0# ./read.SHEnter your name:jellyhello,jelly#./read.SHEnter your Name:sorry,too slow

4.-N implements the count input. Specifies that the read command accepts the input data length. When this length is exceeded, either pressing any key indicates the end of the input.
-n1 indicates that the input of a character is accepted and exits without pressing the ENTER key

#!/bin/Bashread-n1-p"want to continue [y/n]?"Y1 Case$y 1inchY|y)Echo-E"\nok,continue!";; N|n)Echo-E"\nok,stop!";; *)Echo-E"\nerror choice!"Esac# ./read.SHwant to continue [y/n]?yok,continue!# ./read.SHwant to continue [y/n]?Nok,stop!# ./read.SHwant to continue [y/n]?perror Choice!

5.-S implements hidden input. The actual result is that the input data is consistent with the background color. Often used to accept password input.

#!/bin/-S  "Enter you password:"passwdecho" \ n " Echo " haha,your passwd is: $passwd "  ~]#./read. SH Enter you password:                                                 passwd is:aixocm

6. Read the data in the text as input to read

#!/bin/Bashcount=1cat  gg.txt while    Read  name   # Reads the contents of the Gg.txt text row by line to  the variable namedo        echo"$count: $name"          Count=$[$count +1]done#. /read. SH 1 : Taeyeon 2 : Jessica 3: Sunny

[Shell Foundation]--read Command

Related Article

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.