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

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

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

  1. [unit1]
  2. field1=value1
  3. field2=value2
  4.  
  5. [unit2]
  6. field1=value3
  7. field3=value4
  8.  
  9. ...
  10. ...

样例例如以下,config.ini:

  1. [DATABASE]
  2. dbms_ip=localhost
  3. user=root
  4. passwd=cloud
  5. db_name=cloud
  6. port=2394
  7.  
  8. [BUSINESS]
  9. port=9084
  10.  
  11. [OFFLINE]
  12. 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:

  1. #!/bin/bash
  2. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
  3. export PATH
  4.  
  5. #Program
  6. # This program is to modify the configuration file
  7. #History
  8. # 2014.10.30 WeiZheng 1.1
  9.  
  10. MY_HOME="/home/weizheng/10-30-yg/yg-soft"
  11. CONFIG_FILE="$MY_HOME/config.ini"
  12. ERROR_NUM=255
  13.  
  14. function get_line_num()
  15. {
  16. # Check if the argument name has the separator "-" that separate the argument unit and argument field
  17. separator=$(echo $1 | grep "-")
  18. if [ -z "$separator" ]; then
  19. echo -e "error: \"$1\": argument name has no separator \"-\" that separate the argument unit and argument field"
  20. exit $ERROR_NUM
  21. fi
  22.  
  23. # Check if the argument name has argument unit and argument field
  24. arg_unit=$(echo $1 | cut -d "-" -f 1)
  25. arg_field=$(echo $1 | cut -d "-" -f 2)
  26. if [ -z "$arg_unit" -o -z "$arg_field" ]; then
  27. echo -e "error: \"$1\": argument name has no argument unit or argument field around \"-\""
  28. exit $ERROR_NUM
  29. fi
  30.  
  31. # Get the argument unit's interval [$arg_unit_line_num, $next_neighbour_unit_line_num)
  32. arg_unit_line_num=$(grep -n "\[$arg_unit\]" $CONFIG_FILE | cut -d ":" -f1)
  33. if [ -z "$arg_unit_line_num" ]; then
  34. echo -e "error: \"$arg_unit\": can not find argument unit"
  35. exit $ERROR_NUM
  36. fi
  37.  
  38. next_neighbour_unit_line_num=$(awk "NR>$arg_unit_line_num && /^\[.*\]/ {print NR; exit 0}" $CONFIG_FILE)
  39. if [ -z "$next_neighbour_unit_line_num" ]; then
  40. file_line_count=$(wc -l $CONFIG_FILE | cut -d " " -f 1)
  41. next_neighbour_unit_line_num=$((file_line_count+1))
  42. fi
  43. echo "argument unit interval: ($arg_unit_line_num, $next_neighbour_unit_line_num)"
  44.  
  45. arg_field_line_nums=$(grep -n "^$arg_field=" $CONFIG_FILE | cut -d ":" -f1 | tr "\n" "\ ")
  46. if [ -z "$arg_field_line_nums" ]; then
  47. echo -e "error: \"$arg_field\": can not find argument field"
  48. exit $ERROR_NUM
  49. fi
  50. echo "matched argument field in line: $arg_field_line_nums"
  51.  
  52. # the $arg_field_line_num must in the interval ($arg_unit_line_num, $next_neighbour_unit_line_num)
  53. for arg_field_line_num in $arg_field_line_nums
  54. do
  55. if [ $arg_field_line_num -gt $arg_unit_line_num -a $arg_field_line_num -lt $next_neighbour_unit_line_num ]; then
  56. echo "find argument field in line: $arg_field_line_num"
  57. return $arg_field_line_num
  58. fi
  59. done
  60.  
  61. # 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)
  62. echo -e "the argument field is not in the argument unit interval"
  63. exit $ERROR_NUM
  64. }
  65.  
  66. while [ "$1" ]
  67. do
  68. # Check if the separator "=" that separate the argument name and argument value exists
  69. equal_symbol=$(echo $1 | grep "=")
  70. if [ -z "$equal_symbol" ]; then
  71. echo -e "error: \"$1\": argument has no \"=\""
  72. exit $ERROR_NUM
  73. fi
  74.  
  75. # Check if argument name and argument value exist
  76. arg_name=$(echo $1 | cut -d "=" -f 1)
  77. arg_value=$(echo $1 | cut -d "=" -f 2)
  78. if [ -z "$arg_name" -o -z "$arg_value" ]; then
  79. echo -e "error: \"$1\": argument has no name or value around \"=\""
  80. exit $ERROR_NUM
  81. fi
  82.  
  83. # Get the line number of the argument from CONFIG_FILE
  84. get_line_num $arg_name
  85. args_line_num=$?
  86.  
  87. # use sed change the argument line
  88. arg_field=$(echo $arg_name | cut -d "-" -f 2)
  89. sed -i "${args_line_num}c $arg_field=$arg_value" $CONFIG_FILE
  90. new_line=$(sed -n "${args_line_num}p" $CONFIG_FILE)
  91. echo "the argument has been changed: $new_line"
  92. shift 1
  93. done

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

  1. ./modify_config_file BUSINESS-port=8888

输出例如以下:

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

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

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

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

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

  1. MY_HOME="/home/weizheng/10-30-yg/yg-soft"
  2. 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. jquery的confirm插件介绍

    参考:1.http://craftpip.github.io/jquery-confirm/ 2.http://www.bootcdn.cn/jquery-confirm/readme/    3.h ...

  2. Android Http POST文件上传之-----RFC1867协议

    RFC1867协议介绍            RFC1867协议主要是在HTTP协议的基础上为INPUT标签添加了file属性.同一时候限定了Form的method必须为POST,ENCTYPE必须为 ...

  3. RTOS系统与Linux系统的区别

    RTOS是实时操作系统 Linux是时分系统,不过可以通过配置内核改成实时系统 分时操作系统 英文:Time-sharing Operating System  释义:使一台计算机同时为几个.几十个甚 ...

  4. PHP测试用例-前言 1

    前提知识准备 在学习本课程之前,你需要准备以下知识点: 掌握一般的PHP开发技能,使用面向对象的框架开发过三个月以上 会一些JS知识 了解http协议 拥有以下知识会学得更加顺利: 掌握PHPUnit ...

  5. Java定时任务的三种实现方法

    译者注:个人觉得用定时任务来跑垃圾回收不是很好的例子,从译者接触到的项目来看,比较常见的是用定时任务来进行非实时计算,清除临时数据.文件等.在本文里,我会给大家介绍3种不同的实现方法:1.普通thre ...

  6. 【翻译自mos文章】在Oracle单机数据库中定义database service

    来源于: Defining a Database Service with a Stand Alone Database (文档 ID 1260134.1) APPLIES TO: Oracle Da ...

  7. jQuery控制form表单元素聚焦

      CreateTime--2017年5月28日08:57:16Author:Marydon jQuery使form表单的第一个文本框聚焦 /** * 使form表单的第一个文本框聚焦 */ func ...

  8. jQuery 文档操作 - insertAfter() ,insertBefore(),after(),before() 方法

    这个方法跟prependTo()和appendTo()不一样的地方在于,一个是仍然插入到元素内部,而insertAfter和insertBefore是插入到元素外部. 这里拿insertBefore来 ...

  9. Spring AOP事务管理(使用切面把事务管理起来)

    在<Spring Transaction 分析事务属性(事务的基本概念.配置)>基础上 http://blog.csdn.net/partner4java/article/details/ ...

  10. asp.net 复习总结

    1.asp.net页面上格式化时间是:<%# Eval("jsBianhao", "{0:yyyy/MM/dd}")%>