A linux shell script that modifies the configuration file,

Source: Internet
Author: User

A linux shell script that modifies the configuration file,

Not long ago, I found a Blog reading the configuration file.

Now, you need to set up an interface for users to modify similar configuration files through the web interface. The big way is to call linux shell script from php, so, now, you can paste a linux shell that can modify this configuration file.

First, the configuration file format is as follows:

[unit1]field1=value1field2=value2[unit2]field1=value3field3=value4......

For example, config. ini:

[DATABASE]dbms_ip=localhostuser=rootpasswd=clouddb_name=cloudport=2394[BUSINESS]port=9084[OFFLINE]pcap_file=test.pcap

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


The reference blog mentioned above can read such a configuration file. Currently, we need to modify this configuration file through linux shell.

The program we designed is called modify_config_file, which is modified using a format like./modify_config_file unit1-field1 = changed_value1 unit2-field1 = changed_value2 (the parameter can be added further.

It is not difficult to modify the configuration file. You can solve the problem in 20-30 lines. However, based on the principle that "all inputs are harmful", various Error Tolerance processes need to be added to the shell. If the USER parameter input is incorrect, the user should be promptly reminded to locate the problem, the following is a shell based on the original intention. Of course, the name is modify_config_file:

#!/bin/bashPATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbinexport PATH#Program#This program is to modify the configuration file #History#2014.10.30   WeiZheng  1.1MY_HOME="/home/weizheng/10-30-yg/yg-soft"CONFIG_FILE="$MY_HOME/config.ini"ERROR_NUM=255function get_line_num(){# Check if the argument name has the separator "-" that separate the argument unit and argument fieldseparator=$(echo $1 | grep "-")if [ -z "$separator" ]; thenecho -e "error: \"$1\": argument name has no separator \"-\" that separate the argument unit and argument field"exit $ERROR_NUMfi# Check if the argument name has argument unit and argument fieldarg_unit=$(echo $1 | cut -d "-" -f 1)arg_field=$(echo $1 | cut -d "-" -f 2)if [ -z "$arg_unit" -o -z "$arg_field" ]; thenecho -e "error: \"$1\": argument name has no argument unit or argument field around \"-\""exit $ERROR_NUMfi# Get the argument unit's interval [$arg_unit_line_num, $next_neighbour_unit_line_num)arg_unit_line_num=$(grep -n "\[$arg_unit\]" $CONFIG_FILE | cut -d ":" -f1)if [ -z "$arg_unit_line_num" ]; thenecho -e "error: \"$arg_unit\": can not find argument unit"exit $ERROR_NUMfinext_neighbour_unit_line_num=$(awk "NR>$arg_unit_line_num && /^\[.*\]/ {print NR; exit 0}" $CONFIG_FILE)if [ -z "$next_neighbour_unit_line_num" ]; thenfile_line_count=$(wc -l $CONFIG_FILE | cut -d " " -f 1)next_neighbour_unit_line_num=$((file_line_count+1))fiecho "argument unit interval:          ($arg_unit_line_num, $next_neighbour_unit_line_num)"arg_field_line_nums=$(grep -n "^$arg_field=" $CONFIG_FILE | cut -d ":" -f1 | tr "\n" "\ ")if [ -z "$arg_field_line_nums" ]; thenecho -e "error: \"$arg_field\": can not find argument field"exit $ERROR_NUMfiecho "matched argument field in line:  $arg_field_line_nums"# the $arg_field_line_num must in the interval ($arg_unit_line_num, $next_neighbour_unit_line_num)for arg_field_line_num in $arg_field_line_numsdoif [ $arg_field_line_num -gt $arg_unit_line_num -a $arg_field_line_num -lt $next_neighbour_unit_line_num ]; thenecho "find argument field in line:     $arg_field_line_num"return $arg_field_line_numfidone# if not return in for-loop, the arg_field_line_num is not in the interval ($arg_unit_line_num, $next_neighbour_unit_line_num)echo -e "the argument field is not in the argument unit interval"exit $ERROR_NUM}while [ "$1" ]do# Check if the separator "=" that separate the argument name and argument value existsequal_symbol=$(echo $1 | grep "=")if [ -z "$equal_symbol" ]; thenecho -e "error: \"$1\": argument has no \"=\""exit $ERROR_NUMfi# Check if argument name and argument value existarg_name=$(echo $1 | cut -d "=" -f 1)arg_value=$(echo $1 | cut -d "=" -f 2)if [ -z "$arg_name" -o -z "$arg_value" ]; thenecho -e "error: \"$1\": argument has no name or value around \"=\""exit $ERROR_NUMfi# Get the line number of the argument from CONFIG_FILEget_line_num $arg_nameargs_line_num=$?# use sed change the argument linearg_field=$(echo $arg_name | cut -d "-" -f 2)sed -i "${args_line_num}c $arg_field=$arg_value" $CONFIG_FILEnew_line=$(sed -n "${args_line_num}p" $CONFIG_FILE)echo "the argument has been changed:   $new_line"shift 1done

You can run the following command to modify the configuration:

./modify_config_file BUSINESS-port=8888
The output is as follows:

argument unit interval:          (8, 11)matched argument field in line:  6 9 find argument field in line:     9the argument has been changed:   port=8888

The first line indicates that the row number range of the unit of BUSINESS is located. Note that it is an open interval. The second line indicates that all rows matching the field row number may have the same field in multiple units; the third row indicates the row number that eventually falls into the unit interval. The fourth row indicates the modified result of the row.

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

1. ./modify_config_file BUSINESSerror: "BUSINESS": argument has no "="2. ./modify_config_file BUSINESS=error: "BUSINESS=": argument has no name or value around "="3. ./modify_config_file BUSINESS=8888error: "BUSINESS": argument name has no separator "-" that separate the argument unit and argument field4. ./modify_config_file BUSINESS-=8888error: "BUSINESS-": argument name has no argument unit or argument field around "-"5. ./modify_config_file BUSINESS-por=8888argument unit interval:          (8, 11)error: "por": can not find argument field6. ./modify_config_file BUSINE-port=8888error: "BUSINE": can not find argument unit

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:

MY_HOME="/home/weizheng/10-30-yg/yg-soft"CONFIG_FILE="$MY_HOME/config.ini"



Related Article

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.