Shell learns 33 days----about redirection

Source: Internet
Author: User
Tags stdin disk usage

About redirects

Additional redirection operators

Use set-c collocation

Posix shell : execute set -c command to open shell The so-called forbidden override option simple > redirect encountered when the destination file already exists .>| operator can be another noclobber option failed

Provide in-line input << and <<-: use program<< Deli miter, input data can be provided within the Shell script body ; This data is called embedded file . in the default case , Shell variables can be made within the body of the embedded file . command and arithmetic substitution .

#!/bin/bash

Cd/home

Du-s * |

Sort-nr |

Sed 10q |

While read amount name

Do

Mail-s "Disk usage waring" $name <<eof

Greetings , you is one of thetop of ten consumers of disk ...

Unneeded files, as soon as possible

Thanks.

Eof

Done

Analysis : The content of the message is the input data .

If the delimiter is enclosed in any one form of quotation marks , the shell will not process the input Nevin , case :

[Email protected] tmp]# i=5

[email protected] tmp]# cat << ' E ' of

> This is the calue if I: $i

> here is a command sub: $ (echo Hello,world)

> EOF

This is the calue if I: $i

Here is a command sub: $ (echo Hello,world)

The delimiter is not separated by any quotation marks

Cat <<eof

> This is the calue if I: $i

> here is a command sub: $ (echo Hello,world)

> EOF

This is the Calue if I:5

Here is a command sub:hello,world

The second form of the embedded file redirector has a minus sign ending.In this case,all tabs at the beginning(Tab)before passing to the program as input,are removed from the embedded file and the end delimiter(Note:only the tabs that begin will be deleted,The opening space is not deleted).do this,LetShellscripts are easier to read.

Open a file with <> as input and output only

UseProgram<>file,available for read and write operations.The default is to open on standard inputfile.generally,<open a file in read-only mode,and>Open in read-only mode.<>operator opens the given file in both read and write modes.this is handed Programidentify and make full use of;actually,using this operator does not require much support..

File descriptor Processing

Linux uses file descriptors to mark each file object . The file descriptor is a non-negative integer , can uniquely indicate the file opened in the reply .

Bash retains 3 file descriptors

File descriptor

Abbreviation

Describe

0

Stdin

Standard input

1

STDOUT

Standard output

2

STDERR

Standard error

The STDIN file descriptor represents the standard input for the Shell , and for the terminal , the White bird quasi-input is the keyboard .

When using the input redirection symbol (<) , Linux replaces the standard input file descriptor with the specified file redirection .

the STDOUT file descriptor represents the standard shell output . On the terminal , The standard output is the display .

Using the output redirection symbol (>,>>), You can direct the content you want to output to the display from the specified file .

The STDERR file descriptor is used to handle error messages , which represent The standard error output of the shell .

By default STDOUT and STDERR point to the same place , by default , The error message is also output to the monitor output .

REDIRECT Error output

REDIRECT only error as follows : See the STDERR file descriptor set to 2 in the table above

[[email protected] tmp]# ls t 2>error

[[email protected] tmp]# Cat Error

LS: cannot access t: no file or directory

redirect Errors and data :

[[email protected] tmp]# mkdir task

[Email protected] tmp]# CD task/

[[email protected] task]# mkdir task

[[email protected] task]# ls task T 2>error 1>list

[email protected] task]# Cat list

Task

[[email protected] task]# Cat Error

LS: cannot access t: no file or directory

Analysis : If an error occurs , the error message will be placed in the wrong ; if the correct , The output information is placed in the List in .

You can also output STDOUT and STDERR to the same file :

[[email protected] tmp]# mkdir task

[[email protected] tmp]# ls task t&>out

[[email protected] tmp]# cat out

LS: cannot access t: no file or directory

Task

There are two ways to redirect output in a script :

1. Temporarily redirect each line of output

2. Permanently redirect all commands in the script

Look first ---- Temporary REDIRECT

To intentionally generate error messages in a script , you need to redirect the output of a separate line (Echo &"error msg>" &2) to STDERR.

Case :

#!/bin/bash

echo "Error msg" >&2

echo "Normal msg"

Execute script , output result :

Error MSG

Normal msg

Parsing : You must add a & symbols .

By default , Linux directs STDERR to STDOUT. but , If you redirect when you run the script STDERR, all the scripts are directed to STDERR the text will be redirected , Case :

[Email protected] tmp]#/test.sh 2>test

Normal msg

[email protected] tmp]# cat test

Error MSG

Parse : redirect the standard error output of the execution script to test, and in the previous step , "Error msg" is directed to the standard error output .

Second ---- Permanent redirection

If you have a large amount of data in your script that needs to be redirected , You can use the exec command to tell the shell Redirect to a specific file descriptor during script execution :

Bash code :

#!/bin/bash

EXEC 1>testout

echo "Error MSG2"

echo "Normal MSG2"

echo "Error Msg1"

echo "Normal MSG1"

Execution :

[Email protected] tmp]#./test1.sh

[email protected] tmp]# cat Testout

Error MSG2

Normal MSG2

Error MSG1

Normal MSG1

redirect input in the script :

Use the exec command to redirect STDIN to a file on a Linux system :
EXEC 0< testfile

This command tells the Shell to get input from the file testfile , not STDIN.

Extended exec command :

Syntax : exec [program [arguments ...]]

Use :

Replace the shell with a new program , or change the I/O settings of the Shell itself .

Main options :

No

Behavior :

The collocation parameter ---- is to use the specified program instead of the Shell to pass parameters to it . If you only use I/O redirect , It will change Shell the file descriptor itself .

For specific reference :

Http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Shell learns 33 days----about redirection

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.