laitimes

Link Operators in Linux - Improving Programming Efficiency and Performance

author:Refrigeration plant

Linking Linux commands means that multiple commands are combined and executed according to the behavior of the operators used between them.

Command chains in Linux are like writing short shell scripts in the shell itself and then executing them directly from the terminal. Links make it possible to automate processes.

In addition, the unattended machine can be systematically operated with the help of a linked operator.

The purpose of this article [1] is to clarify commonly used chain of command operators. It provides short descriptions and corresponding examples that can increase your productivity and enable you to write concise, meaningful code while sometimes reducing the load on your system.

1. AND Operator (&) – Runs Linux commands in the background

The purpose of the '&' is to let the command run in the background. Just type the command, followed by a space and a "&". You can execute multiple commands at once in the background.

Run a Linux command called "ping" in the background:

ping -c5 www.tecmint.com &
           

Run two or more apt commands in the background at the same time:

apt update & apt upgrade &
           

2. Semicolon Operator (;) – Run multiple commands

The semicolon (;) operator makes it possible to run multiple commands at once, and the execution of the commands occurs sequentially.

apt update ; apt upgrade ; mkdir test
           

The combination of the above commands will first execute the update instruction, then the upgrade instruction, and finally create a "test" directory in the current working directory.

3. AND operator (&&) – Run the second command after success

If the first command executes successfully, that is, the first command has an exit state of 0, then the AND operator (&&) executes only the second command. This command is useful when checking the execution status of the last command. For example, I want to use the links command in the terminal to access the website howtoing.com, but before that, I need to check if the host is online.

ping -c3 www.tecmint.com && links www.tecmint.com
           

4. OR operator (||) – Conditional command execution

The OR operator (||) is much like the "else" statement in programmingThe operator allows you to execute the second command only if the first command execution fails, i.e., the exit status of the first command is "1".

For example, I want to do "apt update" from a non-root account, and if the first command fails, the second "links www.howtoing.com" command will be executed.

apt update || links tecmint.com
           

In the above command, since the user is not allowed to update the system, it means that the exit status of the first command is "1", so the last command "links howtoing.com" is executed.

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

mkdir test || links tecmint.com
           

Here, the user creates a folder "test" in their home directory that the user is allowed to use. The command execution is successful, and the exit status is "0", so the last part of the command is not executed.

5. NOT Operator (!) – Selectively executes commands

The NOT operator (!) is much like the "except" statement. The command will execute all commands except for the conditions provided. To understand this, create a directory "howtoing" in the home directory and "cd" to that directory.

mkdir tecmint 
cd tecmint
           

Next, create several types of files in the folder "howtoing".

touch a.doc b.doc a.pdf b.pdf a.xml b.xml a.html b.html
           

See we've created all the new files in the folder "howtoing".

ls 

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

Now use the rm command to delete all but the "html" file at once in a clever way.

rm -r !(*.html)
           

Just to verify, finally execute. Use the ls command to list all available files.

ls 

a.html  b.html
           

6. AND – OR operator (&& – ||) – Conditional execution of the command

The above operator is a combination of the "AND" and "OR" operators. It's a lot like an "if-else" statement.

For example, let's ping howtoing.com and echo "Verified" if successful, otherwise "Host Down".

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 from the internet and try the same command again.

ping -c3 www.tecmint.com && echo "verified" || echo "Host Down"
           

Sample output:

ping: unknown host www.tecmint.com 
Host Down
           

7. PIPE operator (|) – Simplified output processing

This PIPE operator is useful when the output of the first command acts as input to the second command. For example, pipe the output of "ls -l" to "less" and see the output of the command.

ls -l | less

drwx------  tecmint tecmint  4.0 KB Thu Nov 16 12:03:02 2023 AnyDesk
drwxrwxr-x  tecmint tecmint  4.0 KB Tue Oct 10 10:44:35 2023 bin
drwxr-xr-x  root    root     4.0 KB Wed Nov 24 22:05:09 2021 DEBIAN
           

8. Command Combination Operator {}

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

For example, check if the directory "bin" is available and output the corresponding output.

[ -d bin ] || { echo Directory does not exist, creating directory now.; mkdir bin; } && echo Directory exists.
           

9. Precedence Operator () – manages the order in which commands are executed

The () operator can execute commands in order of precedence.

Command_x1 &&Command_x2 || Command_x3 && Command_x4.
           

In the pseudo-command above, what if the Command_x1 fails? Command_x2, Command_x3, Command_x4 will not be executed, for which we use the precedence operator, as follows:

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

In the pseudo-command above, if Command_x1 fails, Command_x2 also fails, but Command_x3 and Command_x4 are still executed according to the exit state of Command_x3.

10. Join Operator () – Multi-line command connection

As the name suggests, the concatenated operator () is used to concatenate large multi-line commands in a shell. For example, the following command opens the text file test(1).txt.

nano test\(1\).txt
           

Reference

[1] Source: https://www.tecmint.com/chaining-operators-in-linux-with-practical-examples/

Read on