Usage of the read command in Linux

Source: Internet
Author: User
Usage of the read command in Linux 1. read the read command to receive input from the standard input (keyboard) or other file descriptors. after the input is obtained, the read command puts the data into a standard variable. The following is the simplest form of the read command: C code bixiaopeng @ bixiaopengt usage of the read command in Linux 1. simply read the read command to receive input from standard input (keyboard) or other file descriptors. after the input is obtained, the read command puts the data into a standard variable. The following is the simplest form of the read command: C code bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-simple.sh #! /Bin/bash # echo "enter your website: "# read the input from the keyboard read website echo" your website is $ website "exit 0 # exit # directly execute the shell result bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-simple.sh enter your website: www.wirelessqa.com your website is www.wirelessqa.com # If you want to see the execution of each step, you can add-x bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh-x read-simple.sh + echo 'Enter your website: 'Enter your web Site: + read website www.wirelessqa.com + echo 'Your website is www.wirelessqa.com 'your website is www.wirelessqa.com + exit 0 2. keep up with the prompt read-pC code bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-p.sh #1. read a single variable #! /Bin/bash read-p "enter your website:" website # only one variable exists, you can also have multiple echo "your website is $ website" exit 0 # execution result bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-p.sh enter your website: www.wirelessqa.com your website is www.wirelessqa.com #2. read multiple variables bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-p-more.sh #! /Bin/bash # read multiple input variables read-p "enter your name and website:" name website echo "your name is $ name, your websit is $ website "exit 0 # execution result bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh-x read-p-more.sh + read-p 'Enter your name and website: 'name website enter your name and website: bixiaopeng www.wirelessqa.com + echo 'Your name is bixiaopeng, your websit is www.wirelessqa.com 'your name is B Ixiaopeng, your websit is www.wirelessqa.com + exit 0 3. enter the read-t option to specify the number of seconds for the read command to wait. When the time is full, the read command returns a non-zero exit status; there is a potential danger to use the read command. The script is likely to stop waiting for user input. If the script must be executed no matter whether the data is entered, you can use the-t option to specify a timer. C code bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-t.sh bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ cat read-t.sh #! /Bin/bash if read-t 10-p "enter the address of your blog in 10 seconds: "website then if [$ website =" www.wirelessqa.com "] then echo" the address you entered is correct: $ website "else echo" the URL you entered is incorrect: $ website "fi else echo" Sorry, you have timed out. please enter it within 10 seconds! "Fi exit 0 # execution result 1 bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-t.sh please enter the address of Bi Xiaopeng blog within 10 seconds: Sorry, you have timed out, please input in 10 seconds! # Execution result 2 bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-t.sh please enter the address of the blog: weibo.com/wirelessqa you entered the URL is wrong: # execution result 3 bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-t.sh please enter the address of the blog: www.wirelessqa.com you entered in 10 seconds is correct: www.wirelessqa.com 4. count input read-n count when the number of characters entered reaches the predefined number, automatically exit, and assign the input data to the variable 01bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-n.sh02 03bixiaopeng @ BixiaopengtekiMacBook-Pro linuxTest $ cat read-n.sh04 #! /Bin/bash05 06 # Example 1. exit 07 read-n1-p if you only receive one input. do you like the old blog? [y/n]? "Answer08case $ answer in09Y | y) 10 echo" OK, thank you! "; 11N | n) 12 echo" oh, I will continue to work hard! "; 13 *) 14 echo" input error, enter y/n "; 15esac16 17 # Example 2. exit 18 read-n2-p if only two inputs are received. please enter either of the following two characters: "any19echo: $ any "20 exit 021 22 # execution result 23bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-n.sh24 do you like the old blog? [y/n]? N oh, I will continue to work hard! 25. please enter either of the following two characters: ai 5. hide input read-s 01bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-s.sh02bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ cat read-s.sh03 #! /Bin/bash04 # If you want to disable user input from the screen, use read-s05read-s-p "and enter your password: "pass06echo07echo" your password is: $ pass "08 exit 009 10 # execution result 11bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-s.sh12 please enter your password: 13 your password is: bixiaopeng 6. each time you call the read command to read a file, the "one line" text in the file is read. When the file has no readable rows, the read command exits in a non-zero state. How can I pass data in the file to read? Use the cat command and pipe the results directly to the while command containing the read command 01bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ vi read-file.sh02 03bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ cat read-file.sh04 #! /Bin/bash05 06 count = keep cat read-s.sh | while read line # output through the cat read-s.sh file, as the read input to line08do09 echo "$ count: $ line "10 count = $ [$ count + 1] 11done12echo" end "13 exit 014 15 # execution result 16bixiaopeng @ bixiaopengtekiMacBook-Pro linuxTest $ sh read-file.sh171 :#! /Bin/bash182: # to disable user input from the screen, use read-s193: read-s-p. "enter your password:" pass204: echo215: echo "your password is: $ pass" 226: exit 023 ended

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.