Shell Redirection File Descriptor

Source: Internet
Author: User

#!/bin/bashRecently looking at the shell, a variety of puzzles, but to solve the feeling of confusion is still very good. Needless to say, Linux uses file descriptors to identify each file object. The file descriptor is a non-negative integer that uniquely identifies the open file in the session.    Each procedure can have a maximum of 9 file descriptors. Standard file descriptor used by Linux: standard input stdin is 0, standard output stdout is 1, standard error output stderr is 2.    And we can create the file descriptor 3~8 ourselves. The piece of code you'll see today: 1 #!/bin/bash 2 exec 3>&1 3 exec 1>test 4 echo "Something to output file"      ....      .... 5 exec 1>&3 6 echo "Now things should is back"The second line of code uses the file descriptor 3 to point to the location of the file descriptor 1, the location of the standard output, to hold the location of the standard file descriptor 1. The third line of code then redirects the file descriptor 1 to the file test before executing the subsequent code.    The file descriptor 1 is again pointed to its original location when the code is executed to line 5th.    This method is to temporarily redirect output in the script and then restore the output to the usual set of methods ~ or will often use, whistling ~. Or that sentence: Where There is a shell,there is a way! #exit 0

Shell Redirection File Descriptor

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.