Interactive input of linux commands read

Source: Internet
Author: User
Interactive input of linux commands readread is an important 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 prompt statement change... interactive input of the linux command read is an important command for reading 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 "prompt statement" variable name [wang @ localhost desktop] $ vim testcmd. sh #! /Bin/bashread-p "Enter your name:" name1 name2 // A space is required before name1, you can assign multiple variables echo $ name1echo $ name2 [wang @ localhost desktop] $ chmod + x testcmd. sh [wang @ localhost desktop] $. /testcmd. sh Enter your name: william samwilliam AMSam [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: 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 $ nameEnter your name: wangwang // A wang is input, the other is echo $ name. 3.-s does not show back for entering the password, which protects the password. [Wang @ localhost desktop] $ read-n 4-s-p "Enter your name:" name; echo $ nameEnter your name: wang // This is echo $ name 4. -t the number of seconds to wait for the input [wang @ localhost desktop] $ read-n 4-t 2-p "Enter your name:" name; echo $ nameEnter your name: // wait 2 seconds before the script automatically jumps out or writes a script: [wang @ localhost desktop] $ vim testcmd. sh #! /Bin/basffe read-t 2-p "Enter your name:" nametaskecho $ nameelse echo "Timeout! "Fi [wang @ localhost desktop] $./testcmd. sh Enter your name: Timeout! 5.-d user-defined delimiters: enter the user-defined delimiters and end the input. [Wang @ localhost desktop] $ read-d ":"-p "Enter your name:" name; echo $ nameEnter your name: name 6. read [wang @ localhost desktop] $ vim testcmd from the standard input. sh #! /Bin/bashcount = 1cat test. c | while read line // from test. c reads each row and assigns the line variable do echo "$ count: $ line" count = $ [count + 1] done [wang @ localhost desktop] $ cat test. c # include Int main () {int a = 4; (++ a) + = a; printf ("we are the best % d! \ N ", a); return 0;} [wang @ localhost desktop] $./testcmd. sh 1: # include 2: 3: int main () 4: {5: int a = 4; 6 :( ++ a) + = a; 7: printf ("we are the best % d! N ", a); 8: return 0; 9:} Note: The cat test. c above can also be changed to ls, ps, grep, find, and other commands.
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.