List in Unix as a loop

Source: Internet
Author: User
Article Title: List is used as a loop in Unix. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

When developing applications in Unix operating systems, loop statements can be said to account for half of the total. Because the Unix operating system uses the shell programming environment, the loop mechanism is different from other advanced languages in structure, such as the for loop of one of the loop statements. The author will introduce how to use the for loop in the Unix operating system and related precautions to improve the utilization of this for loop.

1. Basic Structure of the for loop.

In the shell environment, a for loop consists of two parts: the loop list and the operation statement. The specific format is as follows:

For variable in list (loop list)

Do

Commands)

Done

From the above structure, we can see that variables and lists are used in the for loop to define the loop list. In addition, include the operations to be executed using the do and done keywords. The function of a variable is to take values from the loop list sequentially and pass the parameter values to the operation statement for related operations. In fact, in learning this for loop, the operation statement is not much difficult, or there is no new content. This is the basic statement of Some unix operating systems. The difficulty lies in the determination of the loop list. This loop list is related to two key parameters for loop operation. One is the number of loops, and the other is the parameter values required for internal operations of the loop. For this reason, when I introduce this for loop, the focus is also on the setting of this loop list. That is to say, as long as the system engineer is proficient in writing this loop list, there will be no problem with other content.

Ii. How do I compile the cyclic list?

Because shell is a friendly integrated programming environment with Unix operating systems, or a part of the operating system. Therefore, the definition of this loop list is much richer than that of the for Loop in advanced languages. For example, you can use many operating system files or parameters as their cyclic conditions. Specifically, System Engineers can set the cycle list according to the following rules.

1. Use the file as the loop list.

Sometimes the list may be large. For example, there are more than 100 entries in total. Although these entries can be written in the for statement loop list in sequence, the length of the Code is greatly increased, thus affecting reading. If you want to change these entries in the future, you need to change the source code. This is obviously troublesome. For example, the system engineer needs to check whether there are 20 files in the user directory that the user wants. The names of the 20 files are irregular. Under normal circumstances, you need to enter the file names one by one. Enter one file name and query it once. This operation has been performed for a total of 20 times, and the workload is obviously very large. If the number of files is increased, the operation will be more troublesome. This problem can be solved in the for loop. That is, you only need to enter the names of the 20 files in the loop list, and then search for related files in the specified directory through the query operation in the operation statement. If yes, save location and other information to a file. This simplifies user operations by performing a for loop. At the same time, it can greatly shorten the query time.

However, one problem encountered at this time is to enter 20 file names in the for loop list. For this reason, the system engineer wants to save the names of these files in the file and then let the for loop read the content from the file. This is convenient and is also conducive to subsequent adjustments. In a for loop, you can read related list information from a file. However, this requires a cat command. As we all know, the purpose of this cat command is to read relevant information from a file. If the system engineer who has used the pipeline operator knows that the result of a command can be used as a parameter of another command. In fact, this principle is also used here, but it does not need to be implemented through pipeline characters. Specifically, this loop list can be defined as follows. For name in 'cat filename.txt '(note that a single quotation mark is required because it is changed to text content after being read ). When the list of for loops is large and there is no rule to follow, although the list items can be listed one by one in the loop list, this is not the method recommended by the author. I believe that in this case, it is best to save the list items in a file as suggested by the author. Then, use the cat command to read the for loop list. At this time, this file is relatively independent from the program, so if you need to adjust the value of this list item in the future, you do not need to modify the source code. This is also considered a highly flexible processing method. If the list items need to be adjusted frequently, you can use this method to improve the flexibility even if there are not many items in the list.

  2. Use System variables as the loop list.

In Unix operating systems, the biggest feature of a for loop is that system variables can be used as a loop list. Take the query operation as an example. You may need to find a file from several specified folders. These directories are determined by environment variables. If a system engineer has just arrived at the enterprise, he will take over from the old system engineer. At this time, the system engineer needs to know some basic configurations of the Unix server system. One of the important contents is the setting of some important environment variables. If you have installed the Oracle database, you need to know the installation path. If the Mail server is installed, you need to know the storage location and backup location of the Mail. If you view these environment variables one by one, the workload will be large. If it is the author, it will not be so inefficient. The author's practice is to write a for loop and then output these important environment variables to a file for backup. This will bring great convenience to the subsequent work of the author. For example, if I want to know the Oracle Installation Directory and the mail environment variables, I can use the following loop list.

For varhome in $ ORACLE_HOME $ MAIL.

 

[1] [2] Next page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.