A Linux shell script that modifies the configuration file

Source: Internet
Author: User

Not long ago, once searched a blog is to read the configuration file, http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html, used to now, feel very convenient, thank the author.

Now, a web interface is required to allow the user to modify a similar configuration file, the big way is to call the Linux shell script from PHP, and now paste a can modify this configuration file of the Linux shell.

First, the format of the configuration file is as follows:

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

Examples are as follows, Config.ini:

[database]dbms_ip=localhostuser=rootpasswd=clouddb_name=cloudport=2394[business]port=9084[offline]pcap_file= Test.pcap

The configuration file contains 3 unit, representing 3 large aspects: Database, business, offline; Each unit has its own field name and field values.


The reference to the blog above is the ability to read such a configuration file, and now we are going to modify this configuration file through the Linux shell.

We designed the program named Modify_config_file, using the./modify_config_file unit1-field1=changed_value1 unit2-field1=changed_value2 Such a format (parameters can continue to be added) to be modified.

The ability to modify the configuration file is not difficult, and 20-30 rows will solve the problem. But based on the "all inputs are harmful" principle, you need to add a variety of fault-tolerant processing in the shell, if the user parameter input error, to be able to prompt users, locate the problem, the following is based on such a purpose of the shell, 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 was 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 $ | 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 $ | cut-d "-"-F 1) Arg_field =$ (echo $ | 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 A Rgument 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 on line: $a Rg_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 F Ield is isn't in the argument unit interval "Exit $ERROR _num}while [" $ "]do# Check if the separator" = "that separate the A Rgument name and argument value existsequal_symbol=$ (echo $ | grep "=") if [-Z ' $equal _symbol "]; THENECHO-E "error: \" $1\ ": argument has no \" =\ "" Exit $ERROR _numfi# Check If argument name and argument value Existarg_na me=$ (echo $ | cut-d "="-F 1) arg_value=$ (echo $ | cut-d "="-f 2) If [-Z] $arg _name "-o-z" $arg _value "]; THENECHO-E "error: \" $1\ ": argument have no name or value around \" =\ "" Exit $ERROR _numfi# Get The line number of the Argum Ent from config_fileget_line_num $arg _nameargs_line_num=$?# with sed change the argument linearg_field=$ (echo $arg _name | c Ut-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

The user modifies the configuration with the following command:

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

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

Where the first line indicates the line number interval where the unit of business is located, note that the interval is open, and the second line represents all matches to the field line number, because there may be the same field in multiple unit, and the third line indicates the field line number that eventually falls into the unit interval. Row four indicates the result of the row being modified.

In addition, it is possible for users to enter non-conforming formats, which are reported and positioned for the following types of errors:

1../modify_config_file businesserror: "Business": argument have no "=" 2. ./modify_config_file business=error: "business=": argument has no name or value around "=" 3. ./modify_config_file business=8888error: "Business": Argument name have no separator "-" that separate the argument unit an D 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, one) error: "Por": Can not find argument field6../mo Dify_config_file busine-port=8888error: "Busine": Can not find argument unit

If you want to apply 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"

A Linux shell script that modifies the configuration file

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.