Link operators in Linux reduce system load and write more short and meaningful code

The meaning of a link in a Linux command is that several commands are executed in combination by the behavior of the operator. Link commands in Linux, some like you write short shell scripts in the shell, and execute directly in the terminal. Links make automatic processing more convenient. Not only that, an unattended machine can run very well with the help of link operators.

10 link operators in Linux

This article is intended to introduce some common link operators, with short descriptions and related examples to help readers increase productivity, reduce system load, and write more short and meaningful code.

1. ampersand operator (&)

The role of '&' is to make the command run in the background. Just follow the command with a space and '&'. You can run multiple commands in the background in one go.

Run a command in the background:

Tecmint@localhost:~$ ping c5 &

Run two commands in the background at the same time:

Root@localhost:/home/tecmint# apt-get update & mkdit test &

2. Semicolon operator (;)

The semicolon operator allows you to run several commands in one go, and the commands are executed sequentially.

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

The above command has performed update and upgrade, and finally created a 'test' folder in the current working directory.

3. with the operator (&&)

If the first command is executed successfully, the AND command (&&) will execute the second command, that is, the first command exit status is 0. (Annotation: The original text here is obviously wrong, we have carried out a translation, interested readers can refer to the original text and the comments below the original text. In UNIX, 0 means no error, and all non-zero 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 in the terminal, but before that I need to check if the host is online or not online.

Root@localhost:/home/tecmint# ping -c3 && links

4. or operator (||)

The or operator (||) is much like the else statement in programming. The above operator allows you to execute the second command if 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, then the second command 'links' will be executed.

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

In the above command, since 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.

If the first command succeeds and the exit status is '0'? Obviously, the second command will not be executed.

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

Here, the user creates a 'test' folder in the home directory, which is allowed. The successful execution of the command, the exit status is '0', so the last command will not be executed.

5. Non-operator (!)

The non-operator (!) is much like the except statement. This command will execute all statements except the ones provided. To understand this, create a directory 'tecmint' in your home directory and 'cd' to it.

Tecmint@localhost:~$mkdir tecmint

Tecmint@localhost:~$cd tecmint

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

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

Take a look at the new file we created in the folder 'tecmint'.

Tecmint@localhost:~/tecmint$ls

A.doc a.html a.pdf a.xml b.doc b.html b.pdf b.xml

Use a clever way to delete all files except 'html' right away.

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 above operator is actually a combination of 'and' and 'or' operators. It is much like the 'if-else' statement.

For example, we ping tecmint.com, if it prints 'verified' successfully, otherwise print 'host failure'.

Tecmint@localhost:~/tecmint$ ping -c3 && echo "Verified" || echo "Host Down"

Sample output

PING (212.71.234.61)56(84)bytes of data.

64bytes from (212.71.234.61): icmp_req=1ttl=55time=216ms

64bytes from (212.71.234.61): icmp_req=2ttl=55time=224ms

64bytes from (212.71.234.61): icmp_req=3ttl=55time=226ms

--- ping statistics ---

3packets transmitted, 3received, 0% packet loss, time2001ms

Rtt min/avg/max/mdev = 216.960/222.789/226.423/4.199ms

Verified

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

Tecmint@localhost:~/tecmint$ ping -c3 && echo "verified" || echo "Host Down"

Instance output

Ping: unknown host

Host Down

7. Pipe operator (|)

PIPE is useful when using the output of the first command as input to the second command. For example, the output of 'ls -l' is piped to 'less' and looks at the output.

Tecmint@localhost:~$ ls -l | less

8. Command merge operator {}

Combine two or more commands, the second command depends on the execution of the first command.

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

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

However, the running result of such a command is not as expected, and the prompt message will always be output. So you need to use the {} operator to merge commands:

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

"The file does notexist"

(Annotation: The original text here should also be copied or written, there are some problems, the example does not appear in the subtitle "{}" operator, so we have modified here)

9. Priority operator ()

This operator allows commands to be executed in order of priority.

Command_x1 &&Command_x2 || Command_x3 && Command_x4.

In the pseudo code above, if Command_x1 fails to execute, none of Command_x2, Command_x3, and Command_x4 will execute. In this case, we use the precedence operator.

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

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

10. Connector ($$)

The connector (), as its name suggests, is used to connect commands in the shell that are too long and need to be split into multiple lines. You can press Enter after entering a "\" and continue to enter the command line until the input is complete. For example, the following command will open the text file test(1).txt.

Tecmint@localhost:~/Downloads$nano test\

1.txt


FTTx-The Last Miles

Fttx-The Last Miles,Fttx - Last Mile Networking Solution,Last-Mile Otdr For Fttx Networks Integrates Opm,Last Mile Fttx Test

Huizhou Fibercan Industrial Co.Ltd , https://www.fibercannetworks.com