Preface: I am learning I/O redirection today.
Objective: To help review the consolidation.
Content:
1.I/O redirection is the change of the source and the output of the input and the,> represents the output redirection,< represents the input redirection (both redirects are overwritten with the contents of the redirected file ,>> represents an append redirect, No append redirection is entered .
The 2.bash has a set of switching functions, which can be switched on to some things, by themselves man. For example, SET-C is enabled to disallow redirection of files that already exist, but even if this function is turned on, it can be forced to add >|. Set +C is to turn off the above functions.
3. Standard input stdin 0, standard output stdout 1, standard error stderr 2. Note: standard input, output, and standard errors are not an output stream.
1) such as: Ls/varr >/tmp/var.out This is not able to execute, prompt ls:/varr:no such file or directory. The output stream and standard input stream that represents the standard specification are not a stream.
Improve the above method: Ls/varr 2>/tmp/var.out
Similarly: Ls/var 2> /tmp/var.out is able to perform (displayed on the screen), but it is not enough to redirect to the standard error to /tmp/var.out
2) If you want the wrong to use the standard error, the correct standard is correct, then
ls/varr >/tmp/var.out 2>/tmp/var.out (Note that these two files can be the same or different)
And one way is to:&>.
such as: Ls/varr &>/tmp/var.out ( Directs both output and errors to a file.
4. Talk about input redirection;
For input redirection not every command can be used, cat can be used, because the Cat single command is to wait for input, TR can also.
means that commands that can be entered are redirected with input.
for example: Cat </etc/fstab can change the contents of the Fstab file into the input part of the cat.
tr ' A-Z ' A-Z ' </etc/fstab
5. Although there is no best input, << can represent the creation of the document.
Cat << End indicates that an end is encountered in the input character.
Here's an important feature:
cat >> filename << eof (this order is not required) indicates that the file filename is to be appended, and that the input ends when EOF is encountered.
6. Talk about pipelines:
A pipeline is a result of a command that can be used by another command.
such as: echo "Hello World" | Tr ' A-Z ' A-Z
echo "Redhat" | passwd--stdin Dongzi
Cut-d:-f1/etc/passwd | Sort
Note: The tee command belongs to an entry two-Exit command.
such as: echo "Hello World" | Tee/tmp/hello.out
Can be displayed once, and then written to a file.
Several exercises for piping:
1) Statistics of the number of documents in the Usr/bin directory;
Answer: Ls/uer/bin |wc-l
2) Remove the shell from all users on the current system and require that each shell be displayed only once.
Answer: cut-d:-f7/etc/passwd|sort-u
3) Think: How to display the content type of each file under the/var/log directory.
Answer: File/var/log.
4) Remove the 6th line of the/etc/inittab file.
The answer is head-6/etc/inittab|tail-1.
5) Remove the user name and shell of the 9th user in the/etc/passwd file, display it to the screen and save it to the/tmp/users file;
Answer: head-9/etc/passwd |tail-1 |cut-d:-f1,7|tee/tmp/users
6) Display all files starting with PA in/etc directory and the number of statistics;
Answer: ls-d/etc/pa*|wc-l
7) without using a text editor, add the contents of the alias CLS = Clear line to the current user. bashrc file;
Answer: Echo "Alias Cls=clear" >>~/.BASHRC
This article from "Tiandaochouqin" blog, declined reprint!
Linux Learning Note II (I/O redirection, Plumbing)