Logical operators & Pipes in Linux

March 24, 2021

Logical operators

Let’s now take a look at logical operators.

Command1 & command2

The first command is started in the background to continue until it has finished; immediately after starting the first command, the second command is started and it will run in the foreground:

$ find / -name "*.z" & ls----------------     -----Command1            command2

In the preceding example, the first command, find, will start running in the background and, while the find command is running in the background, the ls command will start running in the foreground.

Command1 && command2

The second command is only started if the first command is successful. To achieve this, the shell checks the exit (return) status of the first command and starts the second command only if and when that exit status is found to be 0:

$ ls /home/ganesh  &&  echo "Command executed successfully"Since we are working as user ganesh,$ ls /root  && echo "Command executed successfully"

Since we are working as a normal user, we cannot access the /root directory. Therefore, nothing will be printed on screen.

Command1 || command2

The second command is only started if the first command fails. The shell checks the exit status of the first command and starts the second command only if that exit status is not equal to 0:

$ ls /root  ||  echo "Command execution failed"

Example:

$ ls || echo "command ls failed"

In the preceding example, if ls runs successfully, then echo will not be called. If the ls command fails, such as $ ls /root, and if the user is not the root, then ls will fail and the echo command will print command ls failed.

When && or || are used, the exit status of the first command is checked first, and then the decision to perform the next will be taken.

For example:

$ ls$ echo $?  0$ ls /root    ls: /root: Permission denied$ echo $?  1$ tar cvzf /dev/st0 /home /etc | | mail -s "Something went wrong with the backup" root

If we give the command as follows:

$ cd /home/student/work/temp/; rm -rf *

Initially, the shell will change to the /home/student/work/temp folder, and then it will delete all files and folders.

If we enter the command as follows:

$ cd /backup/ol/home/student/work/temp/ && rm * -rf

This will first change to the required folder, and then the rm command will be called for deletion. The problem with ; is that even if the shell fails to change to the required folder, the rm command will execute and it will delete all the files and folders from your original folder. This will be really dangerous.

For example:

$ [[ "a" = "b" ]]; echo okok

In this case, the [[ ]] expression will evaluate to false. Since the semicolon will not check the status of the earlier command, ok will be printed even if the first [[ ]] expression fails.

$ [[ "a" = "b" ]] && echo ok

In this case, the [[ ]] expression will evaluate to false. As the first expression is false, the “&&” operator will not proceed to execute the next command.

In this case, ok will be printed only if [[ ]] is true.

Pipes

We have already used pipes in many earlier examples. It is a tool for inter-process communication:

$ command_1 | command_2

In this case, the output of command_1 will be sent as an input to command_2. The limitation is that the communication is half duplex. This means the data can flow in only one direction. Normally, for inter-process communication, you need to open files and then get the file descriptor. This will be used to write to the pipe file. Again, we need to create a Fifo file with special commands. The preceding technique simplifies this process. We only need to insert | in between the two processes. The operating system creates one intermediate buffer. This buffer is used for storing the data from one command and will be used again for the second command.

A simple example is as follows:

$ who | wc

The preceding simple command will carry out three different activities. First, it will copy the output of the who command to the temporary file. Then the wc command will read the temporary file and display the result. Finally, the temporary file will be deleted.

Normally, there will be two processes. The first command is the writer process. The second process is the reader process. The writer process will write to temp_file and the reader will read from temp_file. Examples of writer processes are ps, ls, and date. Examples of reader processes are wc, cat, grep, and sort.

Related Articles

Calculating and reducing the runtime of a script

In this article, we are going to learn how to calculate and reduce the script’s runtime. A simple time command will help in calculating the execution time.PrerequisitesBesides having a terminal open, make sure you have the necessary scripts present in your...

read more

Lorem ipsum dolor sit amet consectetur

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

two × three =