Shell 13th question (3)

Source: Internet
Author: User
11.5
Dangdang ~~~ Class ~~~ Pai_^
As mentioned above: $ cat <File>; after file, the results of files with original content are washed away ﹗
It is not difficult to understand this concept. This is just a matter of priority:
* In Io redirection, The stdout and stderr pipelines are prepared before they are synchronized from stdin.
Read the data.
That is to say, in the above example,>; file first clears the file and then reads it into <file,
However, at this time, the file has been cleared, so it becomes unable to read any data...
Oh ~~~ It turns out that ~~~~ Pai_^
What about the following two examples?
Code: [copy to clipboard] $ cat <>; File
$ Cat <file >;>; File
Well... students, these two answers are just exercise questions. Please submit your homework before the next lesson ﹗
Okay, I/O redirection is almost finished, sorry, because I only know so much ~~~
XI ~~ Pai_^
However, there are other things that must be discussed. audience members (Please make your own music ~! #@! $ % ):
---- Pipe line ﹗
When talking about pipe line, I believe many people will not be unfamiliar with it:
The "|" symbol we often see on many command lines is pipe line.
But what exactly is pipe line?
Don't worry... check the English-Chinese dictionary first to see what pipe means?
That's right! It means "pipe...
So, can you think like how the sewer pipe goes one after another?
What about the input and output between each pipe?
Hmm ??
A flash of light: the pipe line I/O is exactly the same as the pipe I/O:
* Stdout of the previous command is connected to stdin of the next command ﹗
Indeed... no matter how many pipe lines you use on command line,
I/O of the two commands are connected to each other! (congratulations! Finally! ^_^)
But... what about... stderr?
Good question! but easy to understand:
* What if the water pipe leaks?
That is to say: in pipe line, the stderr of the previous command will not be connected to the next command.
Stdin,
Its output, if not 2>; If exported to file, it is still sent to the monitor ﹗
You must pay attention to this point when using pipe line.
Then, you may ask:
* Is there a way to feed stderr to the stdin of the next command?
(Greedy guy ﹗)
Of course there are methods, and you have already learned! ^_^
I just want to remind you:
* How do you merge stderr into stdout for output together?
If you cannot answer the question, come back and ask me after class... (if your face is really thick enough ...)
Maybe you still want to do it! Maybe you have encountered the following problems:
* In the pipe line section of cm1 | cm2 | cm3 ...,
Is the result saved to a file?
If you write cm1 | cm2>; file | cm3,
Then you will surely find that stdin of cm3 is empty! (of course, you have connected the pipe to another pool ﹗)
Smart users may solve this problem:
Code: [copy to clipboard] cm1 | cm2>; file; cm3 <File
Yes, you can do this, but the biggest disadvantage is: in this way, file I/O will be doubled ﹗
File I/O is the most common killer of command execution.
Any experienced shell operator will try to avoid or reduce the frequency of file I/O.
Is there a better way to solve the problem above?
Yes, that's the tee command.
* The Tee Command copies stdout to the file without affecting the original I/O.
Therefore, the preceding command line can be set as follows:
Code: [copy to clipboard] cm1 | cm2 | tee file | cm3
On the preset, Tee will rewrite the target file. If you want to add more content, you can use the-a parameter to achieve it.
Basically, pipe line is widely used in Shell operations, especially in text
Filtering,
Cat, more, Head, tail, WC, expand, TR, grep, sed, awk ,...
And other text processing tools,
Using pipe line together, you will be surprised that command line was so wonderful ﹗
Often let people look for him in the crowd, suddenly look back, that person is in the dark! "sense... ^_^
....
Now, the introduction to I/O redirection has come to an end.
If you are free in the future, I will introduce you to other things that are fun on Shell! Bye... ^_^
Bytes -------------------------------------------------------------------------------------------------
12) Do you want if or case?
After a pleasant Spring Festival holiday, people also become lazy and dispersed...
However, if you agree to your homework, you still need to stick to it ~~~
Do you still remember the return value we introduced in Chapter 10th?
Yes, what we will introduce next is related to it. If your memories are offset by the happy holidays,
So, we recommend that you go back to study and then come back...
If you remember return value, I think you should also remember & | what does it mean?
Using these two symbols together with command group, we can make shell script
Become smarter.
For example:
Code: [copy to clipboard] comd1 &&{
Comd2
Comd3
} | {
Comd4
Comd5
}
It means:
If the return value of comd1 is true,
But execute comd2 and comd3,
Otherwise, run comd4 and comd5.
In fact, we are writing shell scripts.
Such conditions are often used for different processing actions.
Usage & |
It is true that conditional execution can be achieved. However, it is not so intuitive to understand from the perspective of "human language.
More often, we like to use keyword like if... then... else...
To express conditional execution.
In bash shell, we can modify the previous Code as follows:
Code: [copy to clipboard] If comd1
Then
Comd2
Comd3
Else
Comd4
Comd5
Fi
This is also the if statement we use most frequently in shell scripts:
As long as the command line after if returns true return value (we usually use test
Command to send return value ),
But execute the command after then. Otherwise, the command after else is used to end the sort-based command.
Keyword.
In the if else formula, else does not need to be used, but then is required.
(If you do not want to run any command after then, the ":" NULL command can be used instead ).
Of course, then or else can also be followed by a further layer of conditional attention, which is in Shell
Script design is very common.
If multiple conditions need to be determined in sequence, we can use keyword like Elif:
Code: [copy to clipboard] If comd1; then
Comd2
Elif comd3; then
Comd4
Else
Comd5
Fi
It means:
If comd1 is true, execute comd2 then ﹔
Otherwise, test comd3 again, but execute comd4 prepare ﹔
If neither comd1 nor comd3 is true, comd5.
If statement is common, you can use a lot of shell scripts
As you can see, I will not give an example here...
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.