The use of the eight turns of the Linux shell

Source: Internet
Author: User
Tags save file

First, the file code

"Steering" means that the standard input (such as the keyboard) should be read by other files, and the result should be displayed on standard output (such as screen) and output to other files. The concept of file descriptor is involved in this process.

The so-called file code, refers to the operating system to give the opened file a number (starting from 0), as a tracking file for use, such as the file read or write operations, all use the file code to communicate. The below file code numbers are expressed in FD.

operating system default 3 file code: 0 standard input, 1 standard output, 2 standard error  

These 3 files are opened when the shell environment is set up, and if you open a new file, you assign more than 2 numbers.

< represents the shift input, if the < left omits the file code, the default is 0, which refers to the standard input.

> represents the turn output, if the > left omits the file code, the default is 1, which refers to the standard output.

most of these steering grammars cannot be used alone and must be used with bash commands, such as exec, LS, read, and so on, which are part of the instruction structure .

The FD, N, m in this article represent the file code

#echo ' hello,world! ' > A.txt#exec 7<>a.txt#cat <&7hello,world!


Second, the operation of the document

The "Action file" to be described here refers to the use of the concept of steering, opening the file, copying the file code, and closing the file.

1. Open File

Syntax:fd<> file

Purpose: Open the file and specify the code as FD. Files opened in this way are available for reading and writing.

Use case: Exec 6<>test.txt

Use exec to perform a turn, open file Test.txt, and specify its code file as 6

2. Close the file

After opening the file, if the file is no longer in use, it is best to close the file and return the system resources that the calling file code occupies. It's a good habit.

Turn off the steering input file

Syntax: fd<&-

Use case: 6<&-

Turn off the turn output file

Syntax: fd>&-

Use case: Exec 5>&-

3, copy the file code, establish the file link

Operating system has opened the file code (0, 1, 2), in order not to affect the original file code, it is best to copy one, and so the end of the operation and then restore back, called "Copy code."

Two different ways:

A, copy to the input file code m, save file code n, so that n connected to M n<&m

b, copy the output of the file code m, save the file code n, so that n connected to M n>&m

Case:

EXEC >&6 is equivalent to exec 1>&6, which connects the standard output to the file code 6, so anything that appears in the standard output is dumped into the file code 6

Cat <&6 is the same as cat 0<&6, which shifts the contents of file code 6 to standard input and is displayed by Cat.

EXEC 5<&0 Copy the standard input (0), and save it as a file code 5, that is, to establish a connection to file code 0, simply to do a standard input backup.

EXEC 0<&5 5<&-restores the standard input stored in file code 5 and closes the file code 5.


Third, the use of steering

The code is next to the symbol, and the steering input output is used only once.

1. Steering input

Syntax:fd< file

Purpose: Use "file" as input, specify its file code as FD

Case:

Wc-l </etc/passwd #< left ellipsis fd, default fd is 0, refers to the standard input steering:

EXEC 6</etc/passwd #< left file code is not empty;

Wc-l <&6 #当一次输入回车的时候, output passwd number of rows, then press ENTER again to empty

2. Steering output

Syntax:fd> file

Purpose: Use file for output, specify file code as FD

If the file does not exist, it will be established and will be emptied if it exists. You can close the file default action Set-o Noclobber or Set-c

Case:

>test.txt (Create empty pieces) #> left ellipsis fd,fd default to 1

: >test.txt #效果同上

ls > dirs.txt #存储命令执行的输出

EXEC 6> out.txt #开启文件out. txt as output

echo ' Hello world! ' 1>&6 #将echo输出的内容存储在文件代码6

fd>| file with fd> file but fd>| ignore the noclobber option, it will be forced to overwrite and empty.

>| regardless of whether the Noclobber option is enabled, the content must be emptied as long as the file exists.

3. Turn to attach

Syntax:fd>> file

Purpose: Use the same method as fd> file, just fd>> will not overwrite, not empty, just append.

4, standard error with standard output to do the turn

Syntax:&> file or >& file effect is the same.

' > File 2>&1 ' and &> function as well.


This article is from the "Welcome to Linux World" blog, so be sure to keep this source http://linuxnote.blog.51cto.com/9876511/1641252

How to use the eight turns of the Linux shell

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.