In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article mainly focuses on how to parse the basic knowledge of Shell script and redirect and pipeline operation. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Shell is a special application in Linux system, which acts as a "command interpreter" in Linux system, between the kernel and the user.
Anonymous pipe "|" pipe symbol, as the name suggests, passes data from the entrance of the pipe to the outlet of the pipe, just like a pipe.
The pipeline exists to solve the problem of inter-process communication. it can transfer the data between two processes and transfer the output data of one process to another as its input data. On the left side of the pipeline is the data giver, and on the right side of the pipeline is the data receiver.
For example, echo "abcd" | passwd-stdin username means that the output result "abcd" of the process echo is taken as the input data of the process passwd.
The basic pipe symbols and their usage are easy to understand. The question now is, for ps aux | grep "ssh", why is there a grep process in the result?
[root@xuexi ~] # ps aux | grep ssh root 1211 0.00.1 82544 3600? Ss Jul26 0:00 / usr/sbin/sshd-D root 25236 0.00.2 145552 5524? Ss 05:28 0:00 sshd: root@pts/0 root 25720 0.1 0.2 145416 5524? Ss 06:15 0:00 sshd: root@pts/1 root 25770 25770 112648 948 pts/1 S + 06:15 0:00 grep-- color=auto ssh generally thinks that ps is executed first, and then the output data is passed to grep. At this time, grep is not yet running and ps is finished. Why can you still count the information of the grep process? The reason is that the pipeline implements inter-process communication, and there is an intersection between the two processes. After running the ps process, the grep starts to collect process information and is waiting for data to be received. When ps collects any data, it will put the output into memory and pass it to the grep for filtering.
The essence of the pipe is data transmission, and the output data on the left side of the pipe is put into memory and read by the process on the right side of the pipe. If there is not enough memory to store the output data completely, the process on the left side of the pipe will wait until the right side of the pipe takes out part of the data in memory so that the process on the left side of the pipe can continue to output. The process on the right side of the pipe starts immediately after the process on the left side of the pipe starts, but it has been waiting to receive data from the pipe.
In other words, the processes on the left and right sides of the pipe run almost in no order.
So how can ps aux | grep "ssh" avoid the process of grep itself in the result? There are two ways:
Method 1: ps aux | grep "ssh" | grep-v "grep"
Method 2: ps aux | grep "ss [h]"
[root@xuexi ~] # ps aux | grep ss [h] root 1211 0.00.1 82544 3600? Ss Jul26 0:00 / usr/sbin/sshd-D root 25236 0.00.2 145552 5524? Ss 05:28 0:00 sshd: root@pts/0 root 25720 0.0 0.2 145416 5524? Ss 06:15 0:00 sshd: root@pts/1 method one is to apply the "- v" characteristic of grep, and method two is to apply the characteristics of regular expressions.
In the process of using anonymous pipes, you may have found that the processes on both sides of the pipe belong to the same process group, that is, the data on the left side of the pipe can only be passed to the process on the right side of the pipe, and no other process can read this data. But in addition to anonymous pipes, there are named pipes, named pipes store a process's data in a pipe file (fifo), and other processes can read the pipe file to read the data, that is, no longer restrict the data reader. For named pipes, see the books on Linux/unix operating system kernels or programming classes, which are generally described in detail.
1.8.2 Redirection 1.8.2.1 the most common file descriptors for standard input (stdin), standard output (stdout), and standard error output (stderr) are 0, 1, and 2, respectively, where 0, 1, and 2 can also be considered their numeric codes. For the output information, it can be considered as the information printed on the screen, but the error is not given is the standard output, the error prompt is the standard error output, of course, this description is biased, but easy to understand. You can also customize your own descriptors to implement advanced redirects, which may be used in future articles.
Standard input = / dev/stdin = code 0 =
Standard output = / dev/stdout = code 1 = > or > > symbol.
Standard error output = / dev/stderr = code 2 = use 2 > or 2 > symbols.
Note that the / dev/std {in,out,err} above is the default output targets of 0, 1, and 2, respectively, and when redirected, these targets are no longer used. See the following for details.
, 2 > implements the overlay function, and > >, 2 > > implements the additional function, but pay attention to
Sometimes, the use of "-" also means / dev/stdin. Such as:
[root@xuexi ~] # cat / etc/fstab | cat-the symbols 2 > & 1 and & > and & > are common in scripts, which indicate that both stdout and stderr are redirected to the same place, that is, all output is redirected. Such as the most common "& > / dev/null".
Note:
(1)。 The redirect operation is first parsed by shell, and before executing the command, the redirect will open the file to determine the location of the input and output. If you are redirecting the output operation, first truncate the file to make it empty.
(2). / dev/stdin, / dev/stdout, / dev/stderr and other devices are only the default data flow targets (strictly speaking, the output targets of file descriptors 0, 1, 2), and they are not equivalent to "standard input 0, standard output 1, standard error 2". The reason why it is called "redirection" is to change the flow direction of data and no longer enter into these default devices.
(3)。 The order of redirects is important.
For example, ls / > file1 2 > & 1 means to open file1 as the destination of standard output (fd=1), and then bind standard error to standard output (already file1), so that both standard error and standard output are redirected to file1. It is equivalent to ls / & > file1. The "&" symbol indicates descriptor reuse (fd 2 duplicate from fd 1). Think of it as file descriptor 2 copying file descriptor 1, or file descriptor 2 reusing file descriptor 1 so that fd=2 also points to the file that fd=1 points to.
Ls / 2 > & 1 > file1 means pointing standard error to standard output first, which is still / dev/sdtout (screen), so the output target of standard error is / dev/stdout (screen). Then open file1 as the target of standard output. Therefore, it eventually redirects standard error to / dev/stdout and standard output to file1. You can let the ls command generate errors to test, ls dlfjasl 2 > & 1 > file1, and the results will be displayed directly on the screen * *. **
Throwing stdout or stderr to / dev/null discards the output information, while redirecting / dev/null to a file means emptying the file.
[root@xuexi ~] # cat / dev/null > ab.sh in addition to this, there are several ways to quickly empty files
[root@xuexi ~] # > ab.sh [root@xuexi ~] #: > ab.sh # or "true > ab.sh" In fact, they are equivalent to "> ab.sh" [root@xuexi ~] # echo''> ab.sh [root@xuexi ~] # truncate-s 0 ab.sh # truncate command to shrink and expand the file size [root@xuexi ~] # dd if=/dev/null of=ab.sh again (very important): in the redirect (including error redirection) statement with an output class, the file is truncated to 0 size before the command is executed. So if you are editing a file and redirect the edited results back to the file, there will be an exception because there is no appropriate content for editing after truncation. A simple example is as follows:
[root@xuexi ~] # head a.log > a.log sometimes it is dangerous to overwrite the output directly with ">". You can use set-C to set not to overwrite if the output redirection file already exists. Use set + C to cancel the effect of set-C. If you still want to force an override when set-C is set, you can use "> |" instead of ">" to redirect the output. Similarly, error output also has this feature.
[root@xuexi tmp] # set-C [root@xuexi tmp] # cat flip > ttt.txt-bash: ttt.txt: cannot overwrite existing file [root@xuexi tmp] # cat flip > | ttt.txt [root@xuexi tmp] # set + C1.8.2.2 cat and redirection combined with cat can input content into the file by line.
[root@xuexi tmp] # cat log.txt # override input to log.txt > this is stdin character > eof can also use the following method.
[root@xuexi tmp] # cat > log1.txt this is stdin character first! > eof on the one hand, the eof part must use "it represents here document, and then the input is input to cat as a document. Since it is document, there must be a document Terminator to mark the end of document, using the character after here document, for example, eof. Instead of using eof, it's the same with other characters, but the Terminator of document must be changed accordingly. Such as:
[root@xuexi] # cat 123 > 345 > abcx 12345 on the other hand, > log1.txt means to overwrite the contents of document to the log1.txt file. If you want to append it, use > > log1.txt. Therefore, the append method is as follows:
[root@xuexi tmp] # cat > > log1.txt this is stdin character first! > eof or
[root@xuexi tmp] # cat > log1.txt > this is stdin character first! > eof1.8.2.3 tee dual orientation can use tee double orientation. In general, redirection either inputs information into a file or outputs it to the screen, but it is troublesome to export both to the screen and to a file. This idea can be achieved using the dual orientation feature of tee. As shown in the picture.
Tee [- a] file
Option description:
-a: the default is to overwrite the output to the file, and using this option will become an append behavior.
File: in addition to output to standard output, it will also be output to file. If file is "-", it means that you enter it into standard output again.
For example, the following code saves all the contents of the file starting with a to b.log, while giving the copy to the following cat, which is used to save the content to x.log. Where "-" stands for the preceding stdin.
[root@xuexi tmp] # cat a* | tee b.log | cat-> x.log can also be output directly to the screen:
[root@xuexi tmp] # cat a * | tee b.log | cattee is saved to the file by default by overwriting, and can be appended to the file using the-an option. Such as:
[root@xuexi tmp] # cat a * | tee-a b.log | cat can now display a copy on the screen while creating a file or writing content to a file using cat and redirection.
[root@xuexi tmp] # cat x y > z 1 > eof x y z 1 another tee tip: the fancy usage of tee and pee. It is recommended to have some foundation before reading this article.
1.8.2.4 in bash
Here document has already explained above. For here string, it means that the
For example:
Passwd-- stdin user is equivalent to: echo password_value | passwd-- stdin user above is a tutorial on sharing the basics of Shell scripting and redirecting and piping operations. If you have anything to add, you can leave a comment in the comments section. The above is the Linux system-related content shared by Liangxu tutorial Network for all friends. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! Thank you for your reading. I believe you have some understanding of "how to parse the basics of Shell scripts and redirect and pipeline operations". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.