In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 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 Bash programming to achieve the cycle, the article is very detailed, has a certain reference value, interested friends must read it!
Cycle
All programming languages I have used have at least two loop structures for performing repetitive operations. I often use for loops, but I find that while and until loops are also useful.
For cycle
My understanding is that the for command implemented in bash is more flexible than most languages because it can handle non-numeric values; by contrast, for loops such as standard C can only handle values of numeric types.
The basic structure of the Bash version of the for command is simple:
For Var in list1; do list2; done
Explain: "for each value in list1, set $Var to that value and use that value to execute the program statement in list2; after all the values in list1 are executed, the whole loop ends and exits the loop." The value in list1 can be a simple explicit string value or the result of a command execution (``contains the result of command execution, as described in the second article in this series). I often use this structure.
To test it, make sure that ~ / testdir is still the current working directory (PWD). Delete everything in the directory and take a look at this simple example of a for loop that explicitly writes out a list of values. This list is a mix of letters and numbers-but don't forget that all variables in bash are strings or can be treated as strings.
[student@studentvm1 testdir] $rm * [student@studentvm1 testdir] $for I in abcd1234; do echo $I; doneabcd1234
Give the variable a more meaningful name and become an advanced version of the previous version:
[student@studentvm1 testdir] $for Dept in "Human Resources" Sales Finance "Information Technology" Engineering Administration Research; do echo "Department $Dept"; doneDepartment Human ResourcesDepartment SalesDepartment FinanceDepartment Information TechnologyDepartment EngineeringDepartment AdministrationDepartment Research
Create several directories (show some processing information when you create them):
[student@studentvm1 testdir] $for Dept in "Human Resources" Sales Finance "Information Technology" Engineering Administration Research; do echo "Working on Department $Dept"; mkdir "$Dept" DoneWorking on Department Human ResourcesWorking on Department SalesWorking on Department FinanceWorking on Department Information TechnologyWorking on Department EngineeringWorking on Department AdministrationWorking on Department Research [student@studentvm1 testdir] $lltotal 28drwxrwxr-x 2 student student 4096 Apr 8 15:45 Administrationdrwxrwxr-x 2 student student 4096 Apr 8 15:45 Engineeringdrwxrwxr-x 2 student student 4096 Apr 8 15:45 Financedrwxrwxr-x 2 student student 4096 Apr 8 15:45 'Human Resources'drwxrwxr-x 2 student student 4096 Apr 8 15:45' Information Technology'drwxrwxr-x 2 student student 4096 Apr 8 15:45 Researchdrwxrwxr-x 2 student student 4096 Apr 8 15:45 Sales
The $Dept variable must be enclosed in quotation marks in the mkdir statement; otherwise, a space between the name (such as Information Technology) will be treated as two separate directories. A practical rule that I have always believed in: all files and directories should be one word (with no spaces in the middle). Although most modern operating systems can handle situations where there is a space between names, system administrators need to put extra effort into ensuring that scripts and CLI programs handle these special cases correctly. Even if they are annoying, be sure to consider them, because you never know which files you will have. )
Delete everything under ~ / testdir again-run the following command again:
[student@studentvm1 testdir] $rm-rf *; lltotal 0 [student@studentvm1 testdir] $for Dept in Human-Resources Sales Finance Information-Technology Engineering Administration Research; do echo "Working on Department $Dept"; mkdir "$Dept" DoneWorking on Department Human-ResourcesWorking on Department SalesWorking on Department FinanceWorking on Department Information-TechnologyWorking on Department EngineeringWorking on Department AdministrationWorking on Department Research [student@studentvm1 testdir] $lltotal 28drwxrwxr-x 2 student student 4096 Apr 8 15:52 Administrationdrwxrwxr-x 2 student student 4096 Apr 8 15:52 Engineeringdrwxrwxr-x 2 student student 4096 Apr 8 15:52 Financedrwxrwxr-x 2 student student 4096 Apr 8 15:52 Human-Resourcesdrwxrwxr-x 2 student student 4096 Apr 8 15:52 Information-Technologydrwxrwxr-x 2 student student 4096 Apr 8 15:52 Researchdrwxrwxr-x 2 student student 4096 Apr 8 15:52 Sales
Suppose there is a requirement to list all the RPM packages on a Linux machine and attach a short description to each package. I encountered this demand when I was working for North Carolina. Since open source had not yet been "approved" by the state, and I only used Linux on desktops, my boss (PHB), who knew nothing about technology, asked me to list all the software installed on my computer so that they could "approve" a special case.
How do you achieve it? One way to do this is to know that the rpm-qa command provides a complete description of the RPM package, including what the idiot boss wants: the software name and profile description.
Let's implement the final result step by step. First, list all the RPM packages:
[student@studentvm1 testdir] $rpm-qaperl-HTTP-Message-6.18-3.fc29.noarchperl-IO-1.39-427.fc29.x86_64perl-Math-Complex-1.59-429.fc29.noarchlua-5.3.5-2.fc29.x86_64java-11-openjdk-headless-11.0.ea.28-2.fc29.x86_64util-linux-2.32.1-1.fc29.x86_64libreport-fedora-2.9.7-1.fc29.x86_64rpcbind -1.2.5-0.fc29.x86_64libsss_sudo-2.0.0-5.fc29.x86_64libfontenc-1.1.3-9.fc29.x86_64
Sort the list and print the deduplicated results with the sort and uniq commands (some installed RPM packages have the same name):
[student@studentvm1 testdir] $rpm-qa | sort | uniqa2ps-4.14-39.fc29.x86_64aajohan-comfortaa-fonts-3.001-3.fc29.noarchabattis-cantarell-fonts-0.111-1.fc29.noarchabiword-3.0.2-13.fc29.x86_64abrt-2.11.0-1.fc29.x86_64abrt-addon-ccpp-2.11.0-1.fc29.x86_64abrt-addon-coredump-helper-2.11.0-1.fc29.x86_64abrt -addon-kerneloops-2.11.0-1.fc29.x86_64abrt-addon-pstoreoops-2.11.0-1.fc29.x86_64abrt-addon-vmcore-2.11.0-1.fc29.x86_64
The above command gets the RPM list you want, so you can use this list as input to a loop that eventually prints the details of each RPM package:
[student@studentvm1 testdir] $for RPM in `rpm-qa | sort | Uniq`; do rpm-qi $RPM; done
This code produces extra information. When the cycle is over, the next step is to extract the information the idiot boss needs. Therefore, add an egrep command to search for rows that match ^ Name or ^ Summary. A delimited (^) indicates the beginning of a line, and the entire command indicates that all lines starting with Name or Summary are displayed.
[student@studentvm1 testdir] $for RPM in `rpm-qa | sort | Uniq`; do rpm-qi $RPM; done | egrep-I "^ Name | ^ Summary" Name: a2psSummary: Converts text and other types of files to PostScriptName: aajohan-comfortaa-fontsSummary: Modern style true type fontName: abattis-cantarell-fontsSummary: Humanist sans serif fontName: abiwordSummary: Word processing programName: abrtSummary: Automatic bug detection and reporting tool
In the above command you can try grep instead of egrep and you will find that you can't get the correct results with grep. You can also use the less filter to view the command results through the pipeline. The final command looks like this:
[student@studentvm1 testdir] $for RPM in `rpm-qa | sort | Uniq`; do rpm-qi $RPM; done | egrep-I "^ Name | ^ Summary" > RPM-summary.txt
This command-line program uses pipes, redirects, and for loops, all on one line. It redirects the results of your CLI program to a file that can be used in email or as input elsewhere.
This step-by-step process of building the program allows you to see the results of each step to ensure that the whole program follows your desired process and outputs the results you want.
The idiot boss finally received a list of more than 1900 different RPM packages, which I seriously doubt has been read at all. I gave them what they wanted and didn't hear anything about the RPM package from them.
Other cycles
There are two other types of loop structures in Bash: while and until structures, both of which are similar in syntax and function. The basic syntax of these loop structures is simple:
While [expression]; do list; done
Logical interpretation: when the expression (expression) result is true, the program statement list is executed. Exit the loop when the expression results in false.
Until [expression]; do list; done
Logical explanation: execute the program statement list until the result of the expression is true. Exit the loop when the expression results in true.
While cycle
The while loop is used to execute a series of program statements when the logical expression results in true. Suppose your PWD is still ~ / testdir.
The simplest form of a while loop is the one that runs forever. Conditional statements in the following format always return with true. You can also replace true with a simple 1, and the result is the same, but this explains the use of true expressions.
[student@studentvm1 testdir] $Xanti0; while [true]; do echo $X; Xerox $((Xero1)); done | head0123456789 [student@studentvm1 testdir] $
Now that you have learned all the parts of CLI, make it more useful. First, to prevent the variable $X from having a legacy value after the previous program or CLI command is executed, set the value of $X to 0. Then, because the result of the logical expression [true] is always 1, that is, true, the list of program instructions between do and done will be executed-either until you press Ctrl+C or send a signal 2 to the program. Those program instructions are arithmetic extensions that print the current value of the variable $X and add 1.
One of the tenets of "the Linux philosophy of system administrators" is to pursue elegance, and one way to achieve elegance is to simplify. You can use the operator + + to simplify the program. In the first example, the current value of the variable is printed, and then the value of the variable is increased. You can add a + + to the variable to represent this logic:
[student@studentvm1 ~] $Xroom0; while [true]; do echo $((Xerox +)); done | head0123456789
Now delete the last | head of the program and run it again.
In the following version, the variable increments itself before the value is printed. This is achieved by adding a + operator before the variable. Can you tell the difference?
[student@studentvm1 ~] $Xroom0; while [true]; do echo $((+ + X)); done | head123456789
You have simplified the value of the print variable and self-increment to a single statement. Similar to the + operator, there is also a-- operator.
You need a way to stop the loop when it loops to a particular number. Replace the true expression with a numeric comparison expression to implement it. Here is a program that terminates from loop to 5. In the sample code below, you can see that-le is a numeric logic operator that is "less than or equal to". The meaning of the whole statement: the loop runs as long as the value of $X is less than or equal to 5. When $X increases to 6, the loop terminates.
[student@studentvm1 ~] $Xroom0; while [$X-le 5]; do echo $((Xray +)); done012345 [student@studentvm1 ~] $Until cycle
The until command is very similar to the while command. The difference is that it loops until the value of the logical expression is true. Take a look at the simplest format of this structure:
[student@studentvm1 ~] $Xroom0; until false; do echo $((Xray +)); done | head0123456789 [student@studentvm1 ~] $
It uses a logical comparison expression to count to a specific value:
[student@studentvm1 ~] $X-eq 0; until [$X-eq 5]; do echo $((X +)); done01234 [student@studentvm1 ~] $X-eq 0; until [$X-eq 5]; do echo $((+ + X)); done12345 [student@studentvm1 ~] $above is all the content of the article "how to use Bash programming to achieve loops". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.