不久以前,以前搜到一篇博客是读取配置文件的,http://www.cnblogs.com/bo083/archive/2012/11/19/2777076.html,用到如今,感觉十分方便。感谢作者。

如今,须要通过web界面给用户留出接口来改动类似配置文件,大的方法是从php调用linux shell script,于是,如今贴一个能够改动此种配置文件的linux shell。

首先,配置文件的格式例如以下:

[unit1]
field1=value1
field2=value2 [unit2]
field1=value3
field3=value4 ...
...

样例例如以下,config.ini:

[DATABASE]
dbms_ip=localhost
user=root
passwd=cloud
db_name=cloud
port=2394 [BUSINESS]
port=9084 [OFFLINE]
pcap_file=test.pcap

配置文件里包括3个unit。表示3个大的方面:数据库,业务,离线;每一个unit有属于自己的字段名及字段值。

上文中引用博客正是能读取这种配置文件。而眼下我们便是要通过linux shell来改动这个配置文件。

我们设计的程序名为 modify_config_file,使用 ./modify_config_file unit1-field1=changed_value1 unit2-field1=changed_value2 这种格式(參数能够继续加入)来进行改动。

要做到改动配置文件的功能事实上并不难,20-30行便能够解决这个问题。

可是基于“一切输入都是有害的”的原则,须要在shell中增加各种容错处理,假设用户參数输入错误,要能及时提醒用户。定位问题所在。以下是基于这样一个初衷的shell。当然,名称为modify_config_file:

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH #Program
# This program is to modify the configuration file
#History
# 2014.10.30 WeiZheng 1.1 MY_HOME="/home/weizheng/10-30-yg/yg-soft"
CONFIG_FILE="$MY_HOME/config.ini"
ERROR_NUM=255 function get_line_num()
{
# Check if the argument name has the separator "-" that separate the argument unit and argument field
separator=$(echo $1 | grep "-")
if [ -z "$separator" ]; then
echo -e "error: \"$1\": argument name has no separator \"-\" that separate the argument unit and argument field"
exit $ERROR_NUM
fi # Check if the argument name has argument unit and argument field
arg_unit=$(echo $1 | cut -d "-" -f 1)
arg_field=$(echo $1 | cut -d "-" -f 2)
if [ -z "$arg_unit" -o -z "$arg_field" ]; then
echo -e "error: \"$1\": argument name has no argument unit or argument field around \"-\""
exit $ERROR_NUM
fi # 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" ]; then
echo -e "error: \"$arg_unit\": can not find argument unit"
exit $ERROR_NUM
fi next_neighbour_unit_line_num=$(awk "NR>$arg_unit_line_num && /^\[.*\]/ {print NR; exit 0}" $CONFIG_FILE)
if [ -z "$next_neighbour_unit_line_num" ]; then
file_line_count=$(wc -l $CONFIG_FILE | cut -d " " -f 1)
next_neighbour_unit_line_num=$((file_line_count+1))
fi
echo "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" ]; then
echo -e "error: \"$arg_field\": can not find argument field"
exit $ERROR_NUM
fi
echo "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_nums
do
if [ $arg_field_line_num -gt $arg_unit_line_num -a $arg_field_line_num -lt $next_neighbour_unit_line_num ]; then
echo "find argument field in line: $arg_field_line_num"
return $arg_field_line_num
fi
done # 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 exists
equal_symbol=$(echo $1 | grep "=")
if [ -z "$equal_symbol" ]; then
echo -e "error: \"$1\": argument has no \"=\""
exit $ERROR_NUM
fi # Check if argument name and argument value exist
arg_name=$(echo $1 | cut -d "=" -f 1)
arg_value=$(echo $1 | cut -d "=" -f 2)
if [ -z "$arg_name" -o -z "$arg_value" ]; then
echo -e "error: \"$1\": argument has no name or value around \"=\""
exit $ERROR_NUM
fi # Get the line number of the argument from CONFIG_FILE
get_line_num $arg_name
args_line_num=$? # use sed change the argument line
arg_field=$(echo $arg_name | cut -d "-" -f 2)
sed -i "${args_line_num}c $arg_field=$arg_value" $CONFIG_FILE
new_line=$(sed -n "${args_line_num}p" $CONFIG_FILE)
echo "the argument has been changed: $new_line"
shift 1
done

用户通过下面命令改动配置:

./modify_config_file BUSINESS-port=8888

输出例如以下:

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

当中,第一行表示找到BUSINESS这个unit所在的行号区间,注意是开区间。第二行表示全部匹配到field行号。由于可能多个unit中有同样的field。第三行表示终于落入unit区间的field行号;第四行表示所在行改动后的结果。

另外,用户输入不符合格式是非常有可能。针对下面几种错误都会报告而且定位:

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

假设要应用到其他的配置文件,须要在脚本中改动配置文件所在路径与文件名称:

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

一个改动配置文件的linux shell script的更多相关文章

  1. Linux shell script All In One

    Linux shell script All In One refs xgqfrms 2012-2020 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

  2. Linux shell Script初识

    shell secript: 执行方式的差异: ./ sh执行都是在创建一个子程序来执行,只会继承环境变量, 其中的变量如果export声明,子程序的子程序会继承,不会升级为环境变量 source 的 ...

  3. Linux Shell Script目录

    目录 Linux Shell基础 开始Shell编程 代码 示例代码查看:https://github.com/Furzoom/demo-C/tree/master/src/shell

  4. fastq to tasta using linux shell script

    #!/bin/bash usage() { echo " "; echo "############################################### ...

  5. Linux shell脚本基础学习详细介绍(完整版)二

    详细介绍Linux shell脚本基础学习(五) Linux shell脚本基础前面我们在介绍Linux shell脚本的控制流程时,还有一部分内容没讲就是有关here document的内容这里继续 ...

  6. CentOS Linux下一个tomcat起停,查看日志的shell script

    CentOS 的tomcat安装目录:/usr/local/tomcat vi MyTomcatUitl.sh          创建文件chmod u+x MyTomcatUtil.sh   赋执行 ...

  7. (原创)鸟哥linux学习script shell相关笔记

    在使用鸟哥linux进行script shell学习的过程中碰到一些不太明白的知识点,在这里进行一些记录 1. [root@www scripts]# vi sh03.sh #!/bin/bash # ...

  8. linux基础之Shell Script入门介绍

    本文介绍下,学习shell script编程的入门知识,通过几个入门实例,带领大家走进shell script的神圣殿堂,呵呵,有需要的朋友参考下. 本文转自:http://www.jbxue.com ...

  9. Linux基础之-shell script(变量,运算符,流程控制,函数)

    一.shell script Shell 脚本(shell script),是一种为shell编写的脚本程序.业界所说的shell通常都是指shell脚本,但读者朋友要知道,shell和shell s ...

随机推荐

  1. C#之鼠标模拟技术

    游戏程序的操作不外乎两种——键盘输入控制和鼠标输入控制,几乎所有游戏中都使用鼠标来改变角色的位置和方向,本文主要是讲述如何使用C#调用Windows API函数实现鼠标模拟操作的功能.首先通过结合Fi ...

  2. php之防注入程序绕过浅谈

    <?php/*判断传递的变量是否含有非法字符如:$_POST/$_GET功能:SQL防注入系统*/ //屏蔽错误提示error_reporting(7); //需要过滤的字符 $ArrFiltr ...

  3. Mounting the NFS share on a Windows server

    今天遇到一个相当奇怪的问题,在windows 上mount LINUX NFS, powershell 脚本可以成功, 用图形界面也可以成功,但BATCH就是不行.提示53网络错误. 不过公司已经有人 ...

  4. vim使用指北 ---- Multiple Windows in Vim

    多窗口打开多个文件 vim -o file1 file2 ... ---- 默认上下分割窗口 vim -0n file1 file2 ... ---- vim默认会上下等分n个窗口 分割窗口 :[v] ...

  5. 深入解析淘宝Diamond之客户端架构

    转载:http://blog.csdn.net/u013970991/article/details/52088350 一.什么是Diamond diamond是淘宝内部使用的一个管理持久配置的系统, ...

  6. TP框架I方法详解

    TP框架I方法详解   I方法是ThinkPHP众多单字母函数中的新成员,其命名来自于英文Input(输入),主要用于更加方便和安全的获取系统输入变量,可以用于任何地方,用法格式如下:I('变量类型. ...

  7. 【BIEE】13_BIEE组件介绍(补充)

    Oracle BI Server 该组件主要是管理RPD的,如果该组件不正常,那么Admin Tool将无法联机打开 Oracle交互式信息板 这个组件控制BIEE仪表盘,我们已经建立好的分析.提示等 ...

  8. unsupported major.minor version 解决方法

        转载自http://hi.baidu.com/fatchong/blog/item/191da23b478bbfef15cecbae.html         一直以来都是用jdk1.5,这次 ...

  9. Tomcat几种启动报错及解决办法

    今天真跪了,tomcat的错想到想不到的都遇到了.不记录一下都愧对今天愁掉的hair 在此之前分享一个集错网站,应该是程序员必备的网站之一,不过纯英文,小酸爽 Tags - Stack Overflo ...

  10. 采用CDN加速后,如何在程序里获取用户IP地址

    现在很多网站用了CDN技术,但采用CDN技术后,原来用来获取访问源的IP地址的程序已不能正常使用,它拿到的并不是访问源的真实IP地址,而是CDN节点的IP地址,解决方法是对获取IP的代码作一点小改动. ...