The execution order of the shell

Source: Internet
Author: User

&&,| |, () application of the five symbols of,{},&

When a shell script executes a command, it sometimes depends on whether the previous command succeeded. and && | | is used to determine the effect of the previous command execution.

1 &&
How to use: Cmd1 && cmd2
This is a straightforward way to cmd1 if the return result is true (that is, execution returns to 0, executed successfully), CMD2 is executed
In other words, if CMD1 executes successfully && then executes CMD2
There's an order here.
[-f/home/dellogs.sh] && echo "Clear day to script exists"
In short, the,&& symbol indicates that the second command executes if the first command executes successfully.
2 | |
How to use: Cmd1 | | CMD order 2
|| Command, CMD1 executes CMD2 if no execution succeeds
As a simple example
CP Wopper.txt Oop.txt | | echo "CP command failed to execute successfully"
if | | The command on the left did not execute successfully, and the "CP command execution did not succeed" screen is displayed.
A more practical example is given below.
awk ' {print$1,$5} ' acc.qrt >qtr.tmp | | echo "Sorry the payroll (salary) extraction (extract) did ' Don't work ' | Mail root

To implement a shell's ternary operator:

Cmd1 && CMD2 | | Cmd3

If cmd1 execution succeeds, execute CMD2, otherwise execute CMD3


3 in addition to using && | | These two methods, you can also use (), {} These two symbols to connect the command
() and {} exist only for the purpose of being able to execute a set of commands in a shell or a child shell
How to use (cmd1;cmd2;cmd3; .... ) and {cmd1;cmd2;cmd3; ...}
This situation is less encountered, general and &&,| | Two commands to use together.
For example
Comet Monthent | | (echo "Hello,quess what! Comet did isn't work "| Mail root;exit)

4, use & can put a command in the background to run, so that the shell script can continue to go execution;

For ((i=0;i<10;i++));d o sleep 5 &doneecho "Done"

The above script immediately prints out "done", and the sleep command is thrown to the background to execute without blocking the script execution.

If you want to wait for the previous background command to complete before entering the next loop, you can use the wait command:

For ((i=0;i<10;i++));d o sleep 5 & waitdoneecho ' Done '

This allows you to wait for 50s before you see "done" on the screen.

The execution order of the shell

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.