Standard input/output (read & amp; echo) and shell learning notes for shell programming

Source: Internet
Author: User

Standard input/output (read & echo) of shell programming learning notes and shell learning notes

 

09:32:07

 

Input read:

Purpose:

Read a row from the standard input, or read a row from the file descriptor FD (file descriptor) and divide it into fields.

Usage:

Read [-ers] [-a array] [-d separator] [-I buffer text] [-n read characters] [-N read characters] [-p prompt] [-t timeout] [-u file descriptor] [name...]

Common options:

-A: (arrary) divides the characters obtained by reading into words and stores them by Array (starting from 0)

Source code:

#! /Bin/bashecho "is placed into an Array Based on word segmentation, and the index starts from 0" read-a variableecho "1st: "echo $ {variable [0]} echo" 2nd: "echo $ {variable [1]} echo" 3rd: "echo $ {variable [2]} echo" 4th: "echo $ {variable [3]}
Running effect:

Place words into the array, and the index starts from 0
34 ew qr34 3qr wer er qw
1st:
34
2nd:
Ew
3rd:
Qr34
4th:
3qr

 

-N: receives a specified number of characters. After receiving the specified number of characters, the system immediately exits the input state.

Source code:
#-N read receives 10 Characters
Echo "-n read receives 10 characters"
Read-n 10 num
Echo
Echo "received 10 characters:" $ {num}
Running effect:

-N read accepts 10 Characters
Sgfgsngfdf
The received 10 characters are: sgfgsngfdf


-P: allows you to specify a prompt directly after the read command and assign values to multiple variables.

Source code:
Echo "Use the-p parameter to assign values to multiple variables :"
Read-p "Enter three numbers or characters: (separated by spaces)" num1 num2 num3

Echo "num1 =" $ num1
Echo "num2 =" $ num2
Echo "num3 =" $ num3
Running effect:
Use the-p parameter to assign values to multiple variables: Enter three numbers or characters: (separated by spaces) 23 34 345 num1 = 23num2 = 34num3 = 345
 


-R: do not allow the backslash to escape any characters


-S: Do not display read input data on the screen

Source code:
#-S read input is not displayed on the screen

Echo "-s read input is not displayed on the screen :"
Read-p "password:"-s password

Echo
Echo "password is" $ {password}
Running effect:

-S read input is not displayed on the screen:
Password:
Password is 3432 rq erewr w


-T: the number of seconds for the read command to be input. When the time is full, a non-zero status is returned, and the system exits and waits for the input.

Source code:
#-T read input wait time
Echo "waits for two seconds. After the timeout, the system exits and returns a non-zero value"
Read-t 2 num
Echo $ {num}
Running effect:

Wait for two seconds. After the timeout, the system exits and returns a non-zero value.

 


-U: read from file descriptor FD (file descriptor.

 

  Supplementary content: File descriptor FD file descriptor is a non-negative integer in form. In fact, it is an index value that points to the record table for opening files for each process maintained by the kernel. When the program opens an existing file or creates a new file, the kernel returns a file descriptor to the process. In program design, some underlying programming is usually centered around the file descriptor. However, the file descriptor concept is often only applicable to operating systems such as UNIX and Linux. Traditionally, the file descriptor of the standard input is 0, the standard output is 1, and the standard error is 2. Although this habit is not a feature of the Unix kernel, many applications will not be able to use it because some shells and many applications use this habit.

Example of reading from a file:

 
Source code:# Read accept text as input echo "read accept text as input" cat test. sh | while read line # the output of the cat command is used as the input of the read command. The read value is placed in the line do echo. The received text character is: "$ {line} done.

Running effect:
The text characters received by read as input are: # -------------------------------------------- the received text characters are: # The text characters received by read as input are: echo "read accepts text as input" receives the following text characters: cat test. sh | while read line receives the following text characters: do receives the following text characters: echo "receives the following text characters: "$ {line} receives the following text characters: done

 

09:24:25

 

Output (echo ):

Purpose: output the transmitted content on the standard output.

Usage:

Echo [SHORT-OPTION]... [STRING]...

Common options:

-N: the output content does not wrap.

 
Source code: #-n output text line feed echo-n "this is a test echo"
Running Effect: this is a test echo hello

 

-E: the meaning of the output special character, that is, the meaning of the output character after escape.

 
Source code: #-e outputs special characters echo "hello, \ n my name \ tis ***" echo-e "hello, \ n my name \ tis ***"
 
Running effect:

Hello, \ n my name \ tis ***
Hello,
My name is ***

Special characters and their functions:

Character Function
\ T Insert tab
\ N Wrap the line and move the cursor to the beginning of the line
\ F Line feed but the cursor stays at the original position
\ B Delete the previous character
\ R Move the cursor to the beginning of the line without wrapping
\\ Insert \ character

    

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.