File Descriptors / Streams
__ Standard output / STDOUT (1)
|
|
Standard Input / STDIN (0) -- |
|
|__ Standard Error / STDERR (2)
- numbers above are file descriptors (FD 3 is used for communication to other
processes)
- getting input (STDIN):
tr a-z A-Z < names.list
cat << EOF
hello world
EOF
- redirecting output (STDOUT) redirect:
ls -l > listing.txt # this overrides any content inside the file
cat answers.ptk >> collection.out # this append content (old content is
not overwritten)
- redirecting errors (STDERR)
find / -name *puzzle* 2> /tmp/errors.txt # redirect all errors to a file
sudo backup_files.sh 2> /dev/null # you may also redirect erorrs to
nothing
Summary of redirection operators
- creates a new file containing output
- if that file exists, content will be overwritten
>>
- appends output to a file
- content of original file will not be overwritten
2>
- creates a new file containing the error
- if the file exist, content will be overwritten
2>>
- appends errors to a file
- content of original file will not be overwritten
&>
- creates a new file containing STDERR and STDOUT
- if the file exist, content will be overwritten
&>>
- creates a new file containing STDERR and STDOUT
- content of original file will not be overwritten
- same as: some_command >> file.out 2>&1
<
- sends the content of the specified file to be use as input
<<
- accepts input on the following lines
<>
- causes the file to be used for both STDIN and STDERR
Another redirection tool is tee
command. It allows redirecting STDOUT and STDERR to a file and at the same time displaying STDOUT on the screen. It is used in conjunction with pipe (to be discussed on the next part).
top -b1 | tee /tmp/top.output
sudo find / -type l | tee -a ~/links.txt # `-a` appends output to a file
Pipes
Pipes are used to connect several commands or programs. It can be used
to feed the output of the first command to server as the input to the second.
ps -ef | grep bug # pipes are most common in grep'ing for patterns
cat /etc/passwd | grep apache | cut -d: -f1
No comments:
Post a Comment