Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to connect commands with control operator in Bash

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "how to use control operators to connect commands in Bash". In daily operation, I believe many people have doubts about how to use control operators to connect commands in Bash. Xiaobian consulted all kinds of information and sorted out simple and easy operation methods. I hope to help you answer the doubts about "how to use control operators to connect commands in Bash"! Next, please follow the small series to learn together!

On the command line, use control operators to add logic to compound commands.

Simple compound commands are often used, such as concatenating several commands on a command line. These commands are separated by semicolons to indicate the end of a command. To create a series of simple shell commands on a command line, you simply need to separate each command with semicolons, like this:

command1 ; command2 ; command3 ; command4 ;

*** A semicolon you don't have to add because when you press Enter it indicates the end of a command, but for consistency with the rest, it's better to add it.

All orders are executed without any problems-as long as nothing unexpected happens. But what happens when something goes wrong? We can predict, and we can do that with the built-in && and in Bash.|| Operators track these errors. These two control operators provide some flow control, allowing us to change the order of code execution queues. Semicolons and newlines are also considered control operators for Bash.

The && operator simply means "If Command1 succeeds, Command2 follows." If command1 fails for any reason, command2 will not execute. The syntax looks like this:

command1 && command2

This is permissible because each command returns a value (RC) to the shell indicating whether the command succeeded or failed during execution. Typically, a return value of 0 indicates success, while a positive value indicates a different kind of error. Some system administration tools simply return a 1 to indicate all errors, but many tools use other positive return values to indicate various types of errors.

We can easily use scripts to check the shell variable $?, This can be checked by the next command in the command list, or directly using the system administration tool. Let's look at these return values together. Run a simple command and immediately check its return value, which always belongs to *** a running command.

[student@studentvm1 ~]$ ll ; echo "RC = $? "total 284-rw-rw-r-- 1 student student 130 Sep 15 16:21 ascii-program.shdrwxrwxr-x 2 student student 4096 Nov 10 11:09 bindrwxr-xr-x. 2 student student 4096 Aug 18 10:21 VideosRC = 0[student@studentvm1 ~]$

The return value is 0, indicating that the command was executed successfully. Now try using the same command on some directories we don't have permissions on.

[student@studentvm1 ~]$ ll /root ; echo "RC = $? "ls: cannot open directory '/root': Permission deniedRC = 2[student@studentvm1 ~]$

The meaning of this return value can be found in the man page of the ls command.

Now let's try the && control operator, because it might also be used in a command-line program. We'll start with a simple example: create a new directory and, if successful, create a file in it.

We need a directory where we can create other directories. First, create a temporary directory in your home directory for testing.

[student@studentvm1 ~]$ cd ; mkdir testdir

Create a new directory in ~/testdir, which should also be empty because you just created it, and create a new empty file in this new directory. The following commands can do these things.

[student@studentvm1 ~]$ mkdir ~/testdir/testdir2 && touch ~/testdir/testdir2/testfile1[student@studentvm1 ~]$ ll ~/testdir/testdir2/total 0-rw-rw-r-- 1 student student 0 Nov 12 14:13 testfile1[student@studentvm1 ~]$

We see that everything works fine because the testdir directory is accessible and writable. Then we change the permissions on the testdir directory so that user student no longer has access. The operation is as follows:

[student@studentvm1 ~]$ chmod 076 testdir ; ll | grep testdird---rwxrw-. 3 student student 4096 Nov 12 14:13 testdir[student@studentvm1 ~]$

Use the grep command after the long list (ll) command to list the testdir directory. You can see that user student no longer has access to the testdir directory. Now we run the same command as before, but create a different directory in the testdir directory.

[student@studentvm1 ~]$ mkdir ~/testdir/testdir3 && touch ~/testdir/testdir3/testfile1mkdir: cannot create directory ‘/home/student/testdir/testdir3’: Permission denied[student@studentvm1 ~]$

Although we also get an error message, the && control operator prevents the touch command from running because an error occurred while creating the testdir3 directory. This complex flow control can prevent some mistakes from happening and things from going wrong. But it looks a little bit more complicated.

|| The control operator allows you to add another command that executes when the initial program statement returns a value greater than 0.

[student@studentvm1 ~]$ mkdir ~/testdir/testdir3 && touch ~/testdir/testdir3/testfile1 || echo "An error occurred while creating the directory. "mkdir: cannot create directory ‘/home/student/testdir/testdir3’: Permission deniedAn error occurred while creating the directory. [student@studentvm1 ~]$

When we use && and|| When controlling operators, the syntax format for compound commands that use flow control is usually something like this.

preceding commands ; command1 && command2 || command3 ; following commands

Compound commands that use control operators can precede or follow other commands that are related to control operator flow control but are not affected by control operator flow control. All commands will be executed regardless of what happens in the flow control of the compound command.

These flow control operators make it more efficient to handle errors and notify us in commands when something goes wrong. I use them directly on the command line and also in scripts.

You can delete this directory and its contents as root.

[root@studentvm1 ~]# rm -rf /home/student/testdir Here, the study of "how to connect commands with control operators in Bash" is over. I hope you can solve your doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report