This article is a mirror article of machine translation, please click here to jump to the original article.

View: 13610|Reply: 1

[linux] A Linux shell scrip{filter}t that modifies the configuration file

[Copy link]
Posted on 3/19/2017 10:35:19 PM | | |
Now, you need to leave an interface for users to modify similar configuration files through the web interface, the main way is to call the Linux shell scrip{filter}t from PHP, so now paste a linux shell that can modify this configuration file.

First, the configuration file is formatted as follows:
Examples are as follows, config.ini:

The configuration file contains 3 units, representing 3 major aspects: database, business, offline; Each unit has its own field name and field value.


The blog mentioned above can read such a configuration file, and for now we are going to modify this configuration file through the Linux shell.

We designed a program called modify_config_file and modified it using a format like ./modify_config_file unit1-field1=changed_value1 unit2-field1=changed_value2 (parameters can be added as needed).

It is not difficult to achieve the ability to modify the configuration file, 20-30 lines can solve the problem. However, based on the principle of "all inputs are harmful", it is necessary to add various fault tolerance processing to the shell, if the user parameter input is wrong, to be able to remind the user in time and locate the problem, the following is based on such an original intention of the shell, of course, the name is modify_config_file:


The user modifies the configuration with the following command:

The output is as follows:

Among them, the first line indicates that the line number interval where the BUSINESS unit is located, pay attention to the opening interval; The second line represents all match to field line numbers, as there may be multiple units of the same field; The third line indicates the field line number that finally falls into the unit interval; The fourth line represents the result of the revision of the row.

In addition, it is very likely that the user input does not conform to the format, and the following errors will be reported and targeted:


If you want to apply it to other configuration files, you need to modify the path and file name of the configuration file in the script:







Previous:.sh file execution error: -bash: xxx: /bin/sh^M: bad interpreter: No such file or direc...
Next:The shell modifies the parameter values separated by the equal sign
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com