Go The usage of read in the shell is detailed

Source: Internet
Author: User

The usage of read in the shell is detailed

Original: http://blog.csdn.net/jerry_1126/article/details/77406500

The common usage of read is as follows:

Read-[pstnd] var1 var2 ...

-P Prompt Statement
-N Character Count
-S shielded Echo
-T wait time
-D Input Demarcation

[Plain]View PlainCopy
  1. Read # reads a line from standard input and assigns a value to a specific variable reply
  2. [Email protected]~# Read
  3. hello,world!
  4. [Email protected]~# Echo $REPLY
  5. hello,world!
  6. Read name # reads input from standard input and assigns a value to the variable name
  7. [Email protected]~# Read name
  8. Jerry
  9. [Email protected]~# Echo $name
  10. Jerry
  11. Read Var1 var2 # The first variable is placed in Var1, and the second variable is placed in the VAR2
  12. [Email protected]~# read FirstName LastName
  13. Jerry Gao
  14. [Email protected]~# echo "FirstName: $firstname LastName: $lastname"
  15. Firstname:jerry Lastname:gao
  16. ). Read-p "Text" # print hint ' text ', wait for input, and store input in reply
  17. [Email protected]~# read-p ' please Enter your name:--> '
  18. Please Enter your Name:-->jerry
  19. [Email protected]~# Echo $REPLY
  20. Jerry
  21. Read-p "text" var # print hint ' text ', wait for input, and store input in Var
  22. [Email protected]~# read-p ' please Enter your name:--> ' name
  23. Please Enter your Name:-->jerry
  24. [Email protected]~# Echo $name
  25. Jerry
  26. Read-p "Text" var1 var2 # Print hint ' text ', wait for input, store variables in var1,var2 ...
  27. [Email protected]~# read-p ' What your name? ' FirstName LastName
  28. What your name? Jerry Gao
  29. [Email protected]~# echo "Firstname: $firstname Lastname: $lastname"
  30. Firstname:jerry Lastname:gao
  31. ). Read-r line # allow input with backslash
  32. [Email protected]~# read Line # without the-r parameter; backslashes are not displayed
  33. This was line 1. \ This was line 2.
  34. [Email protected]~# echo $line
  35. This was line 1. This was line 2.
  36. [Email protected]~# read-r Line # with-r parameter; The backslash is displayed normally
  37. This was line 1. \ This was line 2.
  38. [Email protected]~# echo $line
  39. This was line 1. \ This was line 2.
  40. ). Read-t 5 # Specifies a read wait time of 5 seconds
  41. [Email protected]~# read-t 5-p ' Your name: ' Name
  42. Your Name:jerry
  43. [Email protected]~# echo $name # cannot be entered if 5 seconds have not been entered
  44. Jerry
  45. Read-a Arrayname # Read the list of words into the arrayname array
  46. [Email protected]~# read-a citys
  47. BJ SH CD GZ
  48. [Email protected]~# echo ${citys[*]}
  49. BJ SH CD GZ
  50. [Email protected]~# echo ${citys[0]}
  51. BJ
  52. Read-s-P "pwd:" PWD # using the-s parameter can not display the user's input
  53. [Email protected]~# read-p "Enter Your Password:"-S Password
  54. Enter Your Password:
  55. [Email protected]~#
  56. [Email protected]~# echo $PASSWORD # The password you just entered is: 1234
  57. 1234
  58. one). Read-n 1-p "sure? (y/n): "# Use-N to determine the number of arguments
  59. [Email protected]~# read-n 1-p "is you sure? (y/n): "ANSWER
  60. Is you sure? (y/n): Y
  61. [Email protected]~#
  62. [Email protected]~# echo-e "Your ANSWER is: $ANSWER"
  63. Your ANSWER Is:y
  64. ). Read-d ":" var # using: As input delimiter
  65. [Email protected]~# read-d ";"-P "Enter Your name:" Name
  66. Enter Your Name:jerry Gao;
  67. [Email protected]~# echo-e "Your Name: $name"
  68. Your Name:jerry Gao



Read in the script application:

Traversal mode one:

[Python]View PlainCopy
    1. #!/bin/bash
    2. count=0
    3. While Read line
    4. Do
    5. Echo-e "$count:--> $line"
    6. count=$[count + 1]
    7. Done </etc/passwd

Traverse mode Two:

[Python]View PlainCopy
      1. #!/bin/bash
      2. Awk-f: ' {print $1,$7} '/etc/passwd | While read user bash
      3. Do
      4. Echo-e "user= $user; Bash= $bash "
      5. Done

Go The usage of read in the shell is detailed

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.