How to control the execution sequence of linux shell commands

Source: Internet
Author: User

&&
Command 1 & command 2: If the command is successfully executed, run the command.
[Jb]/usr/xxxx/ytcclb> ls-l test. SQL
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL
[Jb]/usr/xxxx/ytcclb> cp test. SQL test. bak & echo "coping file OK! "
Coping file OK!
[Jb]/usr/xxxx/ytcclb> ls-l test .*
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. bak
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL

Move the file and delete the directory:

Copy codeThe Code is as follows: $ mv/apps/bin/apps/dev/bin & rm-r/apps/bin

Sort the files and output them to another file. After successful sorting, print:

Copy codeThe Code is as follows: $ sort test. SQL> test. bak & lp test. bak

|
Command 1 | command 2: If this command fails to be executed | then run this command.

Copy codeThe Code is as follows: $ cp test. SQL test. bak | echo "errors occured during coping file"

For example:
[Jb]/usr/xxxx/ytcclb> cp test. SQL test. bak
[Jb]/usr/xxxx/ytcclb> ls-l test .*
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. bak
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL
[Jb]/usr/xxxx/ytcclb> chmod u-w test. bak
[Jb]/usr/xxxx/ytcclb> ls-l test .*
-R-xr -- r -- 1 xxxx group 225 Nov 15 test. bak
-Rwxr -- r -- 1 xxxx group 225 Nov 15 test. SQL
[Jb]/usr/xxxx/ytcclb> cp test. SQL test. bak | echo "errors occrued during coping file"
Cp: unable to create file test. bak: Permission denied (error 13)
Errors occrued during coping file
[Jb]/usr/xxxx/ytcclb>

() And {}: combine several commands.
(Command 1; command 2;...): execute a group of commands in the current shell.
{Command 1; command 2;...}: The content in braces is executed as a whole.

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.