Shell Scripting Learning Summary--file descriptors and redirects

Source: Internet
Author: User

The file descriptor is an integer associated with the input and output of the file, which is used to track open files, and the file descriptor 0,1,2 is reserved by the system.

0--stdin (standard input)

1--stdout (standard output)

2--stderr (standard error)

echo a1>CP A1 A2; CP  Cat A *cat: a1:permission denieda1a1

Direct 0 to Std.txt, 2 to Err.txt

$ cat A * >std.txt 2>err.txt

Both 0 and 1 are directed to the OUT.txt

$ cat A * >out.txt 2>&1.txt

Tee command

  Redirects the stdin to text and prints it on the screen.

Cat a*| Tee OUT.txt Cat  cat  out.txt a1a1

Note: OUT.txt does not contain "cat: A1:permission denied" because it can only be read from stdin.

The default tee command overwrites the file, but provides a-a option to append.

Cat a*| Tee-a out.txt

Text block inside the redirect script
#/bin//nolog <<eof >log.txtconn Scott/Tigerselectcat; Commit;exiteof

The content between <<eof >log.txt and EOF will be redirected to Log.txt as Stdin,stdout.

Custom file descriptors

    • Create a file descriptor to write to

4>echo newline >&4cat  output.txt newline

    • Creates a file descriptor for reading

echo Hello >
3<input.txtcat <&3Hello

Shell Scripting Learning Summary--file descriptors and redirects

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.