Linux input/output redirection and file search value grep command
I. file descriptor
Linux shell commands can reference some files through file descriptors. Generally, the file descriptors are 0, 1, and 2. In Linux, there are actually 12 file descriptors. the commonly used file descriptors are as follows:
File |
File descriptor |
Input File-standard input |
0 (the default value is the keyboard. If it is 0, it is the output of a file or other commands) |
Output file-standard output |
1 (the screen is used by default. If it is set to 1, the file is used) |
Error output file-standard error |
2 (the screen is used by default. If the screen is 2, the file is used)
|
Ii. File redirection: changes the input and output of the program running, that is, the input source and output location. The specific definitions are as follows:
Output redirection:
Command> filename |
Redirects the standard output to a new file. If filename does not exist |
Command> filename |
Redirects the standard output to a file (append) |
Command> filename |
Redirects the standard output to a file. |
Command> filename 2> & 1 |
Redirects standard output and errors to a file. |
Command 2> filename |
Redirects a standard error to a file. |
Command 2> filename |
Redirects the standard output to a file (append) |
Command> filename2> & 1 |
Redirects standard output and errors to a file (append) |
Input redirection:
Command <filename> filename2 |
The Command uses the filename file as the standard input and the filename2 file as the standard output. |
Command <filename |
The Command uses the filename file as the standard input. |
Command <delimiter |
Read from standard input and know the delimiter |
Bind redirection:
Command> & m |
Redirects the standard output to the file descriptor m. |
Command <&- |
Disable Standard Input |
Command 0> &- |
Same as above |
Iii. Some advanced shell usage
1: Output all standard errors to the linux black hole, which is known as a null device (/dev/null)
[Root @ localhost ~] # Ls/home/ins 2>/dev/null
[Root @ localhost ~] #
2: redirect the standard output and standard error to out. put.
[Root @ localhost ~] # Ls/home/ind> out. put 2> & 1
[Root @ localhost ~] # Cat out. put
Ls: cannot access/home/ind: No such file or directory
[Root @ localhost ~] #
3: Use standard input to write "hello world" in the.txt File"
[Root @ localhost ~] # Cat> a.txt <eof
> Hello world!
> EOF
[Root @ localhost ~] # Cat a.txt
Hello world!
4: create a user awk and use the standard input to set the password to 123456.
[Root @ localhost ~] # Useradd awk
[Root @ localhost ~] #
[Root @ localhost ~] # Echo 123456 | passwd -- stdin awk
Changing password for user awk.
Passwd: all authentication tokens updated successfully.
[Root @ localhost ~] # Su-inds
4. grep command
Grep is a powerful text search tool in Linux. It can use regular expressions to search for text and print matching rows. Grep stands for Global Regular Expression Print, which indicates the Global Regular Expression version. Its permission is granted to all users.
[Options] main parameters:
-C: only counts matching rows are output.
-I: It is case-insensitive (only applicable to single characters ).
-H: When querying multiple files, the file name is not displayed.
-L: When querying multiple files, only names containing matching characters are output.
-N: displays matching rows and row numbers.
-S: the error message that does not exist or does not match the text is not displayed.
-V: displays all rows that do not contain matched text.
Main Parameters of the regular expression pattern:
\: Ignore the original meaning of special characters in regular expressions.
^: Match the start line of the regular expression.
$: Matches the end row of the regular expression.
\ <: Starts from the row that matches the regular expression.
\>: Ends with the row that matches the regular expression.
[]: A single character. For example, [A] indicates that A meets the requirements.
[-]: Range, such as [A-Z], that is, A, B, C Until Z all meet the requirements.
. : All single characters.
*: It can contain 0 characters.
Simple Example
1. filter all rows ending with shutdown in the/etc/passwd file
[Root @ localhost ~] # Grep shutdown $/etc/passwd
Shutdown: x: 6: 0: shutdown:/sbin/shutdown
2. </eof
In/etc/passwd, search for the row where the awk is located, color the awk, and display the row number.
[Root @ localhost ~] # Grep-n awk -- color = auto/etc/passwd
45: awk: x: 1158: 1001:/home/awk:/bin/bash
[Root @ localhost ~] #
Grep uses concise and Regular Expressions
Shell programming in Linux -- basic use of the grep command
Grep command details and related examples
Linux basic commands-grep
Set grep to highlight matching items
Linux grep command learning and summary
Example of 14 grep commands
This article permanently updates the link address: