Basic Linux tutorial-command execution sequence control and Pipelines

Source: Internet
Author: User

Basic Linux tutorial-command execution sequence control and Pipelines
Introduction

Efficient use of Linux skills, such as sequential execution, select execution, pipeline, cut command, grep command, wc command, and sort command.

1. Control the command execution sequence 1. Execute multiple commands in sequence

Generally, we can only enter one command on the terminal each time. Press enter to execute the command. After the execution is complete, we enter the second command and press enter to execute the command ...... You may encounter the following scenarios:apt-getInstall a software, and then immediately run the installed software (or command tools) after the installation is complete ), the software source that happens to be replaced by your host has not updated the Software List (for example, in our environment, every time we start the experiment again, We Have To sudo apt-get update, you can perform the following operations:

$ Sudo apt-get update # Wait ---------- and then enter the following command $ sudo apt-get install some-tool # Wait ---------- and then enter the following command $ some-tool

At this time, you may want to make it possible for me to input all the commands at a time and let it execute each command at its own time. This is the problem we need to solve in this section.

You can use;For example, you can:

$ Sudo apt-get update; sudo apt-get install some-tool; some-tool # Run it on its own
2. Select an execution command

I wonder if you have thought about the above operation. If we have asked it to automatically execute commands in sequence, the previous command fails to be executed, the subsequent commands depend on the results of the previous command, so it takes time to get an incorrect result, and sometimes you cannot determine whether the result is correct. Therefore, we need to be able to execute commands selectively. For example, if the previous command is successfully executed, we need to continue the next one, or if it is not successful, What else should we do? For example, we usewhichTo check whether a command is installed. If yes, run the command. Otherwise, nothing will be done (although this operation has no practical significance, it can help you better understand some concepts ):

$ which cowsay>/dev/null && cowsay -f head-in ohch~

If you have not installedcowsayYou can execute the above command first, and you will find nothing happens. After you install the command again, you will also find some surprises.

The above&&It indicates that if the preceding command execution result (not the content output by the terminal, but the result of the command execution status) returns 0, the execution is followed, otherwise, you can$?Environment Variables get the returned results of the last command:

Users who have learned the C language should know in the C Language&&The table is logical and has||Indicates the logic or, and the Shell also has||The difference between them is that the two symbols in shell can be used not only to represent logic and or, but also to implement simple control of the command execution sequence here.||In this example&&In contrast, when the execution result of the previous command is 0 ($? 0), execute the following command:

$ which cowsay>/dev/null || echo "cowsay has not been install, please run 'sudo apt-get install cowsay' to install"

In addition to the above basic usage, we can also combine this&&And||To implement some operations, such:

$ which cowsay>/dev/null && echo "exist" || echo "not exist"

I will draw a flowchart to explain the above process:

Thoughts

As mentioned above&&And||In combination, are they in any order? For example&&Before||Can this problem be reversed? Is there a problem?

For more details, please continue to read the highlights on the next page:

  • 1
  • 2
  • Next Page

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.