10 best examples of command link operators in Linux

Source: Internet
Author: User

Links in Linux commands mean that several commands are combined and executed through the operator. In Linux, the link commands are similar to writing short shell scripts in shell and directly executed on the terminal. Link makes automatic processing more convenient. Moreover, an unattended machine can run in a very organized manner with the help of the link operator.

10 link operators in Linux

This article aims to introduce some common link operators, and help readers improve productivity, reduce system load, and write more concise and meaningful code through brief descriptions and examples.

1. and operator (&)

'&' Is used to run commands in the background. Just keep a space and '&' behind the command '&'. You can run multiple commands in the background.

Run a command in the background:

tecmint@localhost:~$ ping ­c5 www.tecmint.com &

At the same time, run two commands in the background:

root@localhost:/home/tecmint# apt-get update & mkdit test &
2. semicolon operator (;)

The semicolon operator allows you to execute several commands in one breath in sequence.

root@localhost:/home/tecmint# apt-get update ; apt-get upgrade ; mkdir test

The above commands have successively executed update and upgrade, and finally created a 'test' folder in the current working directory.

3. and operators (&&)

If the first command is successfully executed, and the operator (&) will execute the second command, that is, the first command exit status is 0. The original text is clearly wrong. We have modified the translation. If you are interested, you can refer to the original text and the comments below the original text. In UNIX, 0 indicates no error, and all non-0 return values are various errors ). This command is useful when checking the execution status of the last command.

For example, I want to use the links command to access the website tecmint.com on a terminal, but before that, I need to check whether the host is online or not.

root@localhost:/home/tecmint# ping -c3 www.tecmint.com && links www.tecmint.com
4. or operator (|)

Or the operator (|) is similar to the else statement in programming. The preceding operator allows you to execute the second command when the first command fails. For example, the exit status of the First Command is 1.

For example, I want to execute 'apt-get Update' in a non-root account. If the First Command fails, the second command 'links www.tecmint.com 'will be executed '.

tecmint@localhost:~$ apt-get update || links tecmint.com

In the preceding command, because the user is not allowed to update the system, this means that the exit status of the First Command is '1', so the last command 'links tecmint.com 'will be executed.

What if the first command is successfully executed and the exit status is '0? Obviously, the second command is not executed.

tecmint@localhost:~$ mkdir test || links tecmint.com

Here, the user creates a 'test' folder in the Home Directory, which is allowed. The command is successfully executed and the exit status is '0'. Therefore, the final command is not executed.

5. Non-operator (!)

Non-operator (!) Similar to the except T statement. This command will execute all statements except the provided conditions. To understand this, create a directory 'teccint' in your home directory and 'cd' it.

tecmint@localhost:~$ mkdir tecmint tecmint@localhost:~$ cd tecmint

Next, create different types of files under the 'teccint' folder.

tecmint@localhost:~/tecmint$ touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html

Let's take a look at the new file we created in the 'teccint' folder.

tecmint@localhost:~/tecmint$ ls a.doc  a.html  a.pdf  a.xml  b.doc  b.html  b.pdf  b.xml

Use a smart way to immediately delete all files except 'html.

tecmint@localhost:~/tecmint$ rm -r !(*.html)

Verify the last execution result and use the ls command to list all visible files.

tecmint@localhost:~/tecmint$ ls a.html  b.html
6. And or operator (&-|)

The preceding operators are actually a combination of the 'and' or 'operators. It is similar to the 'if-else' statement.

For example, if we ping tecmint.com, if 'verified 'is successfully printed, otherwise 'host failed' is printed '.

tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "Verified" || echo "Host Down"
Sample output
PING www.tecmint.com (212.71.234.61) 56(84) bytes of data. 64 bytes from www.tecmint.com (212.71.234.61): icmp_req=1 ttl=55 time=216 ms 64 bytes from www.tecmint.com (212.71.234.61): icmp_req=2 ttl=55 time=224 ms 64 bytes from www.tecmint.com (212.71.234.61): icmp_req=3 ttl=55 time=226 ms --- www.tecmint.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199 ms Verified

Now, disconnect our current network connection, and try the same command again.

tecmint@localhost:~/tecmint$ ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"
Instance output
ping: unknown host www.tecmint.com Host Down
7. pipeline operator (|)

PIPE is useful when the output of the first command is used as the input of the second command. For example, the output of 'LS-L' is pipe-to 'ld' and you can see the output.

tecmint@localhost:~$ ls -l | less
8. Command merge operator {}

Merge two or more commands. The second command depends on the execution of the first command.

For example, check whether the parent file 'xyz.txt 'is in the Downloads directory. If it does not exist, create it and output a prompt.

tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz.txt ] || touch /home/tecmint/Downloads/xyz.txt; echo "The file does not exist"

However, if the running result of such a command is not as expected, a prompt is always output. Therefore, you need to use the {} operator to merge the command:

tecmint@localhost:~$ [ -f /home/tecmint/Downloads/xyz1.txt ] || {touch /home/tecmint/Downloads/xyz.txt; echo "The file does not exist"}“The file does not exist”

(Note: The original text should also be copied or written, and some problems have occurred. In this example, the "{}" operator in the title is not displayed, so we have modified it here)

9. Priority operator ()

This operator allows commands to be executed in priority.

Command_x1 &&Command_x2 || Command_x3 && Command_x4.

In the preceding pseudo code, if Command_x1 fails to be executed, None of Command_x2, Command_x3, and Command_x4 will be executed. In this case, we use the priority operator.

(Command_x1 &&Command_x2) || (Command_x3 && Command_x4)

In the preceding pseudo code, if Command_x1 fails to be executed, Command_x2 will not be executed, but Command_x3 will continue to be executed, and Command_x4 will depend on the exit status of Command_x3.

10. connector ()

A connector (), as its name says, is used to connect commands that are too long in shell and need to be divided into multiple lines. You can press enter after entering a "\", and then enter the command line until the input is complete. For example, the following command opens the example file test(1).txt.

tecmint@localhost:~/Downloads$ nano test\1.txt

Today, I will start another interesting article recently. Keep following us. Do not forget to give valuable feedback in the comment column.

Ten Linux commands that are rarely known-Part 2

Ten Linux commands that are rarely known-Part 3

11 Linux commands that are rarely known but useful

Linux Command cd

Cat for Linux commands

Linux Command alias/unalias

Linux Command Parsing: su root and su-root

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.