Chapter 6 of linuxshell programming guide ------ command execution sequence

Source: Internet
Author: User
When executing a command, it sometimes depends on whether the previous command is successfully executed. For example, assume that you want to copy all the files in a directory to another directory, and then delete all the files in the source directory. Before deletion, you want to be sure that the copy is successful. otherwise

When executing a command, it sometimes depends on whether the previous command is successfully executed. For example, assume that you want to copy all the files in a directory to another directory, and then delete all the files in the source directory. Delete
Previously, you wanted to be sure that the copy was successful. otherwise, all files may be lost.

If you want to execute another command after successfully executing a command, or execute another command after a command fails, & | this function can be completed. The corresponding commands can be system commands or shell scripts. Shell also provides methods for executing a group of commands in the current shell or sub-shell, that is, using () and {}.

The general format of use & is:
Command 1 & Command 2

This command execution method is quite direct. & After the command on the left (Command 1) returns true (that is, 0 is returned and successfully executed), the command on the right (Command 2) can be executed. In other words, "If this command is successfully executed & execute this command ".
$ Mv/apps/bin/apps/dev/bin & rm-r/apps/bin

In the above example, the/apps/bin directory will be moved to the/apps/dev/bin directory. if it is not successfully executed, the/apps/bin directory will not be deleted.

In the following example, the file quarterend.txt is first sorted and output to the file quarter. sorted. only after the command is executed successfully will the file quarter. sorted be printed:
$ Sort quarter_end.txt> quarter. sorted & lp quarter. sorted

Use |:
Command 1 | Command 2

| Has different functions. If | the command on the left (Command 1) is not successfully executed, execute | the command on the right (Command 2); or in other words, "If this command fails to be executed | then execute this command ".

[Root @ localhost huangcd] # cp gsdf.txt opps.txt | echo "if you are seeing this cp failed"

Cp: Unable to stat your gsdf.txt ": no file or directory
If you are seeing this cp failed

Here is a more practical example. I want to extract 1st and 5th domains from an audit file and output them to a temporary file. if this operation fails, I want to receive an email:

[Root @ localhost huangcd] # awk '{print $1, $5}' acc. qtr> qtr. tmp | echo "sorry hte payroll extraction didn't work" | mail root

Here, we can use the system command line to execute a shell script named comet for the monthend.txt file first. if the script is not executed successfully, the shell will end.
$ Comet month_end.txt | exit

Use () and {} to combine the command:

If you want to combine several commands for execution, shell provides two methods. You can execute a group of commands in the current shell or sub-shell.

To execute a group of commands in the current shell, you can use the command separator to separate each command and enclose all the commands in parentheses.

It generally takes the following form:
(Command 1; Command 2 ;...)

If {} is used to replace (), the corresponding command will be executed as a whole in the sub-shell instead of the current shell, only when the output of all commands in {} is redirected as a whole, the commands are put into the sub-shell for execution. Otherwise, the commands are executed in the current shell. It generally takes the following form:
{Command 1; Command 2 ;...}

I rarely use these two methods separately. Generally, I only use both methods with & or |.

Return to the preceding example of the comet script. if the script fails to be executed, I may want to execute more than two commands, not just one. I can use these two methods. This is the original example:
$ Comet month_end.txt | exit

If the script fails to be executed, I want to send an email to myself first and then exit. you can use the following method to implement this:

[Root @ localhost huangcd] # eomet month_end.txt | (echo "hello, guess what! Comet did not work "| mail root; exit)

In the preceding example, if only the command delimiters are used without combining them, shell will directly execute
The Last Command (exit ).

Let's look back at the previous example of using & sorting. below is the original example:
$ Sort quarter_end.txt> quarter. sorted & lp quarter. sorted

Use the command combination method. if the sort command is successfully executed, you can copy the output file to a log area before printing.

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.