Monday 24 February 2014

BASH IO redirection

IO redirection:
IO redirection will capture a file , commands, program, scripts’s output, and send it to another file, commands and scripts.

Background: every script has three standard file discriptor:
       stdin: standard input
       stout: standard output
       stderr: standard error output

IO redirection example:
Output redirection
> file: output to the target file, it will overwrite the file content.
>> file: output to the target file, it will append the content at the end of the file
>| file : override the file even with noclobber option

stderr redirection
2>newfile: redirect the stderr to the newfile, for example: ls –al zz 2>newfile.
              If no file matched. The error msg will be written to newfile

stdin redirection
script < file: use file as my stdin instead of default stdin

<<delimiter – redefine the delimiter
For example
#cat > mytest<<GO
>this is my test
>it is ok
>let’s GO
>GO
#cat mytest
this is my test
it is ok
let’s GO

block redirction

> outputfile and < input file. It can used to change the stdin or stdout temporarry. 

No comments:

Post a Comment