redirection advanced with pipe and file io
################################

comm short for command/program name
a good program to test all of this with is:
service –status-all , this has different output for error and for stdout

commands that start with * is going to work with bash 4 and newer
commands that start with # should work with bash 3 and older

There are 2 ways redirections will save to a file, either overwrite, where it will make a new file if needed and if file exists it will remove its contents first then write the new stuff in it (all previous contents will be gone), the other mode is append mode, new stuff goes to the end of the file.

That doesnt apply to pipes

PIPES
#######

# comm1 | comm2
pipe only works on stdout not stderr, so it takes stdout and converts it to stdin for command2

* comm1 |& comm2
pipe now converts both stdout and stderr from comm1 into stdin for comm2, so now grep see

FILE IO
########

# comm1 > file
comm1 standard out goes to file (overwriting existing file), while stderr goes to default place (monitor/terminal/putty/your shell)

# comm1 2> file
comm1 standard error goes to file (overwriting existing file), while stdout goes to default place (monitor/terminal/putty/your shell)

# comm1 >> file
comm1 standard out goes to file (appending to existing file, or making new file), while stderr goes to default place (monitor/terminal/putty/your shell)

# comm1 2>> file
comm1 standard error goes to file (appending to existing file, or making new file), while stdout goes to default place (monitor/terminal/putty/your shell)

# comm1 > file 2> file.err
comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (overwrite mode), and stderr will go to file.err (overwrite mode).
# comm1 >> file 2>> file.err
comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (append mode), and stderr will go to file.err (append mode).

# comm1 >> file 2> file.err
comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (append mode), and stderr will go to file.err (overwrite mode).
NOTE: cant write to the same file like this, behavior is weird

# comm1 > file 2>> file.err
comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to files, stdout will go to file (overwrite mode), and stderr will go to file.err (append mode).
NOTE: cant write to the same file like this, behavior is weird

# comm1 > file 2> file
# comm1 > file 2>&1
* comm1 &> file

comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to the same file. stdout and stderr will be written to file in (overwrite mode).

NOTE: all 3 commands will work, last command will work in bash 4

# comm1 >> file 2>> file
# comm1 >> file 2>&1
* comm1 &>> file

comm1 output will not be shown at all on the screen as all forms of output (stdout and stderr) are redirected to the same file. stdout and stderr will be written to file in (append mode).

NOTE: all 3 commands will work, last command will work in bash 4
NOTE: no such thing as “# comm1 >> file 2>>&1” instead its just “# comm1 >> file 2>&1”, why because if it was “>> and 2>>&1” then that would mean that “>> and 2>&1” exists, but that behaviour is unstable

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *