Tag: Terminal message exists time archive consumer Ror mit Mail
About redirects
Additional redirection operators
Use set-c collocation
POSIX Shellprovides options to prevent accidental file truncation:Runset-ccommand to openShellthe so-called forbidden override option,When it is open again,simple.>REDIRECT encountered when destination file already exists,it will fail .. >|the operator can alsoNoclobberInvalid option.
Provide in-line input << and <<-: use program<< Deli miter, ability to provide input data within the shell script body ; This data is called embedded file . in the default case , Shell ability to make variables within the body of an 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 message content is the input data .
Assuming that the delimiter is enclosed in any one form of an argument , the shell does not process the input Nevin , the 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 of the arguments
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
An additional form of the embedded file redirector has a minus sign ending.such a case,tabs with all openings(Tab)before passing to the program as input,are removed from the embedded file and the end delimiter(Note:only tabs with the beginning will be deleted,The opening space is not deleted).do this,LetShellscripts are easier to read.
Opening a file with <> as input and output is only used
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 Descriptive descriptor processing
Linux uses file description descriptors to mark each file object . the descriptive descriptor of a file is a non-negative integer , ability to uniquely identify the file opened in the reply .
Bash retains 3 descriptive descriptors of the file
Descriptive descriptors for documents |
Abbreviation |
Descriptive narrative |
0 |
Stdin |
Standard input |
1 |
STDOUT |
Standard output |
2 |
STDERR |
Standard error |
STDIN File Description descriptor represents The standard input of the Shell, for the terminal , the White bird quasi-input is the keyboard .
When using the input redirection symbol (<) , Linux replaces the standard input file descriptive descriptor with the redirected specified file .
the STDOUT file Description descriptor represents the standard shell output . On the terminal , The standard output is the display .
Use the output redirection symbol (>,>>) to direct the content that will be output to the display from the specified file .
The STDERR file Description 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
Only redirect Errors , such as the following : See the STDERR File Description descriptor in the table above is set to 2
[[email protected] tmp]# ls t 2>error
[[email protected] tmp]# Cat Error
LS: unable to access T: no file or folder
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: unable to access T: no file or folder
Analysis : Assuming an error , the error message is put into error ; assuming correct , The output information is placed in the List in .
It is also possible to 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: unable to access T: no file or folder
Task
There are two ways to redirect output in a script :
1. Temporarily redirect each line of output
2. All commands in the permanent redirect script
Look first ---- temporarily redirect
For the benefit of generating error messages in the script , a separate line (Echo &error msg> &2) output will need to be redirected to STDERR.
Case :
#!/bin/bash
echo "Error msg" >&2
echo "Normal msg"
Run the script and output The result :
Error MSG
Normal msg
Parsing : You have to add a between the file description descriptor number and the output redirect symbol. Span style= "Font-family:times New Roman" >& symbols .
By default , Linux directs STDERR to STDOUT. but , Assuming that the script was executed with the redirect STDERR, all 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
Analysis : redirect the standard error output from the running script to test, and in the previous step , "Error msg" is directed to the standard error output .
Another ---- Permanent redirection
Assuming that there is a lot of data in the script that needs to be redirected, you can use the exec command to tell the shell Redirect to a specific file descriptive descriptor during script run :
Bash code :
#!/bin/bash
EXEC 1>testout
echo "Error MSG2"
echo "Normal MSG2"
echo "Error Msg1"
echo "Normal MSG1"
Run :
[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 pairing ---- is to use the specified program instead of the shell to pass the parameter to it . Suppose it's just a I/O redirect , It will change Shell The file description descriptor itself .
Detailed references :
Http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html
Shell learns 33 days----about redirection