Shell Input and Output

Source: Internet
Author: User
Tags control characters
1. ECHO Displays a line of strings to the standard output.
0) some details of the ECHO command are different in System V, BSD, and Linux like-Unix systems, which are mainly Linux systems.
1) usage: Echo [-E] [-N] string
① String: string is the string to be output. It can contain shell variable names, escape characters, and so on. It is generally enclosed in double quotation marks.
②-E: Linux ECHO does not explain the Escape Character in string by default, unless this option is added.
③-N: Echo outputs newline (line feed) after the string is output by default. ECHO does not output newline.
2) transfer characters supported by ECHO:
The/nnn ascii code is an NNN (octal) character. If NNN is not a reasonable value, it will be printed literally.
/A bell/B back/C remove the ending nweline character
/F form feed/N line feed/R press ENTER
/T horizontal Tab/V vertical TAB/backslash
3) Example: ECHO-e "User: $ user/tuid: $ uid" // Display User: zqf uid: 500

2. Read reads information from a line of text on the keyboard or file and assigns it to a variable.
1) usage: Read varible1 varible2...
2) if only one variable is specified, read will assign all the content of the input line to the variable until the first filename terminator or carriage return is encountered.
3) if multiple variables are specified, read uses space (environment variable IFS) as the separator to divide the input line into multiple fields and assign them to each variable.
4) The number of fields separated by input text is greater than the number of variables given by read. Read assigns all the extra-long parts to the last variable. 3. Cat is a simple and common command that can be used to display the file content, create a file, and display control characters.
1) show file content: CAT myfile | more // cat command will not stop at the file tab, it will display the complete file
2) create a file: CAT file1 file2 file3> bigfile // create a file named bigfile, which contains the content of the three files.
Cat> myfile // create a new file and enter some content into it. After Entering the content, press end.
3) display the control character in the file: CAT-V filename 4. The pipe can pass the output of one command to another as the input. PIPE is represented by vertical bars "|.
1) usage: command1 | command2
2) Example: ls | grep "*. c" // use the LS command output as the grep command input, that is, search for the C source program file in the current directory.
3) SED, awk, grep and other programs are suitable for pipelines, especially in shell command lines. 5. Tee: It transfers one copy of the output to the standard output, and the other copy to the corresponding file.
1) usage: Tee-a filename
①-A: append to the end of the file.
② The Tee command should be used in combination with the Pipeline
2) Example: the output of the WHO | tee who. Out // who command will not only be output to the standard output, but also to the file who. out 6. Standard input, output, and error
1) file descriptor: The file descriptor is an integer starting from 0 and points to a specific data stream related to the process. When a process is started
File descriptors, corresponding to three standard I/O: standard input (file descriptor 0), standard output (file descriptor 1), standard error (
File descriptor 2 ).
2) standard input (stdin): it is the input of the command. It is associated with the keyboard of the terminal by default.
3) standard output (stdout): it is the output of the command, which is associated with the terminal screen by default.
4) standard error (stderr): it is the output of the command error message, which is also associated with the terminal screen by default.
5) if the process opens an additional file for input and output, it is set to the next available file descriptor, from 3 to 9. 7. File redirection
1) when executing a command, the standard input, output, and errors of the command are associated with file descriptors 0, 1, and 2, and file descriptors 0, 1, and 2 are associated with the terminal by default. If you want the command to read standard input from the file or write the standard output of the command to the file instead of the screen, you need to use file redirection.
2) Redirect standard output
① Cmd> filename: redirects the standard output of the CMD command to a file (if the file exists, its content will be overwritten ).
② Cmd> filename: redirects the standard output of the CMD command to a file (append the end Of the file ).
③> Myfile: Create an empty file with a length of 0. If the file exists, clear the file.
④ Example: ls-L | grep ^ d> files. Out // write the list of subdirectories in the current directory to file files. Out.
> Zqf. Log // clear the log file zqf. Log
⑤ Cmd> filename is actually equivalent to cmd 1> filename. CMD> filename is equivalent to cmd 1> filename.
3) Redirect standard input
① CMD <FILENAME: Use the filename file as the standard input for the CMD command.
② CMD <delimiter: reads the input from the standard input until the delimiter Delimiter is encountered. (Here-document)
③ Example: Sort <grade.txt // sorts the file grade.txt (SORT)
④ CMD <FILENAME is actually equivalent to cmd 0 <FILENAME, CMD <FILENAME and CMD 0 <FILENAME is equivalent.
4) redirection standard error
① Cmd 2> filename: redirects the standard error of the CMD command to a file (if the file exists, its content will be overwritten ).
② Cmd 2> filename: redirects the standard error of the CMD command to a file (append the end Of the file ).
③ Example: Find/-name "*. tmp"-exec Rm-RF {}/; 2>/dev/null // discard the command error message output
5) combined with standard output and standard errors
① Cmd 1> file1 2> file2: redirects the output to file1 and redirects the standard error to file2.
② CMD <file1> file2: Use the file1 file as the standard input for the CMD command, and use the file2 file as the standard output.
③ Example: Find/-name "*. tmp"-print 1> find. out 2> find. Err // write the search result of find to a file
Find. Out, and write the error message of the find command (for example, you do not have sufficient permission to search some Directories) to the find. Err file.
Cat 2.txt // actually writes 1.txt content to 2.txt, which is equivalent to file copy.
6) Merge standard output and standard errors
① Cmd> filename 2> & 1: redirects the standard output along with the standard error to a file.
② Cmd> filename 2> & 1: redirects the standard output along with the standard error to a file (append)
③ Example: grep "standard" *> grep. out 2> & 1 // search for the string "standard" in all text files in the current directory"
④ Cmd> filename 2> & 1 can be viewed as Part 2, "> FILENAME" (redirection standard output) and "2> & 1" (deduplication of standard errors)
To the standard output ).
7) when using commands that accept the file name as a parameter, sometimes the command regards the file descriptor as a file name parameter and reports an error. General file description
There must be no space between the operator and the redirection symbol.
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.