今天跟同事探讨了一下 shell 脚本中对配置文件的读写问题。在此总结一下常用的配置文件的读写方式。
大多数的配置文件都是以key=value形式存在的。配置项完全由键值对组成。这样的配置文件读写也是最简单的,假如有以下配置文件user.conf

id=1
name=shaoqiu
phone=1234567890

1. source

配置的读取很简单,只要将其source进来即可:

shaoqiu@shaoqiu-HP440:~/project/shell$ cat setup.sh
#!/bin/bash source user.conf
echo "id = $id"
echo "name = $name"
echo "phone = $phone" shaoqiu@shaoqiu-HP440:~/project/shell$ ./setup.sh
id = 1
name = shaoqiu
phone = 1234567890
shaoqiu@shaoqiu-HP440:~/project/shell$

2. sed

上面的方法看似简单,但可能会有问题,shell的赋值语句=号两边是不能有空格的,万一用户不小心多加了空白符呢。为防止这样的情况出现,还是换另一种写法比较安全:

shaoqiu@shaoqiu-HP440:~/project/shell$ cat user.conf
id=1
name=shaoqiu
phone=1234567890 shaoqiu@shaoqiu-HP440:~/project/shell$ cat setup.sh
#!/bin/bash function get_config() {
local configPath=$1
local configName=$2
sed -n 's/^[[:space:]]*'$configName'[[:space:]]*=[[:space:]]*\(.*[^[:space:]]\)\([[:space:]]*\)$/\1/p' $configPath
} function set_config() {
local configPath=$1
local configName=$2
local confgValue=$3
sed -i 's/^[[:space:]]*'$configName'[[:space:]]*=.*/'$configName'='$confgValue'/g' $configPath
} get_config user.conf name
set_config user.conf name qiushao
get_config user.conf name
shaoqiu@shaoqiu-HP440:~/project/shell$ ./setup.sh
shaoqiu
qiushao
shaoqiu@shaoqiu-HP440:~/project/shell$ cat user.conf
id=1
name=qiushao
phone=1234567890 shaoqiu@shaoqiu-HP440:~/project/shell$

3. section 配置文件读取

假如配置文件是由多个 section 组成的呢,就像下面这样:

[id=1]
name=shaoqiu
phone=1234567890 [id=2]
name=shaojiang
phone=5678901234 [id=3]
name=zhaotong
phone=8901234567

需要根据输入的id来读写相应的配置项。这样就不能简单的使用前面介绍的方法了。遇到这种情况,可以使用下面这种方法,将配置文件拆分成多个,分别存放到不同的目录中:

config
-id1
-user.conf
-id2
-user.conf
-id3
-user.conf

根据id读取不同目录下的配置文件即可。如果配置信息很多的话,推荐使用这种方法,目前在82平台上的机型配置就是使用这种方法来实现的。但若是配置信息很少,且可能有其他脚本也使用到了这个配置文件的时候,拆分配置文件可能就行不通了,需要寻找其他方法。要读写这种格式的配置文件比较复杂,下面是我使用的方法:

shaoqiu@shaoqiu-HP440:~/project/shell$ cat user.conf
[id=1]
name=shaoqiu
phone=1234567890 [id=2]
name=shaojiang
phone=5678901234 [id=3]
name=zhaotong
phone=8901234567 shaoqiu@shaoqiu-HP440:~/project/shell$ cat setup.sh
#!/bin/bash function string_trim()
{
echo "$1" | sed 's/^[[:space:]]*\(.*[^[:space:]]\)\([[:space:]]*\)$/\1/g'
} function get_region() {
local configPath=$1
local userID=$2
cat -n $configPath | grep "\\[id=.*\\]" | grep -A 1 "\\[id=$userID\\]" | awk '{print $1}' | xargs
} function get_config() {
local configPath=$1
local userID=$2
local configName=$3 local region=$(get_region $configPath $userID)
local startLine=$(echo $region | awk '{print $1}')
local endLine=$(echo $region | awk '{print $2}')
string_trim $(sed -n "${startLine}, ${endLine} s/\(${configName}.*=.*\)/\1/p" $configPath | awk -F= '{print $2}')
} function set_config() {
local configPath=$1
local userID=$2
local configName=$3
local confgValue=$4 local region=$(get_region $configPath $userID)
local startLine=$(echo $region | awk '{print $1}')
local endLine=$(echo $region | awk '{print $2}')
sed -i "${startLine}, ${endLine} s/${configName}.*=.*/${configName}=${confgValue}/g" $configPath
} get_config user.conf 2 name
set_config user.conf 2 name qiushao
get_config user.conf 2 name
shaoqiu@shaoqiu-HP440:~/project/shell$ ./setup.sh
shaojiang
qiushao
shaoqiu@shaoqiu-HP440:~/project/shell$

这种方法的思想是先找出指定id的配置所在的区域,即从哪行开始,到哪行结束。只要找到这个区间就好办了,因为sed可以指定只处理的区间。获取区间的方法解释如下:

cat -n $configPath #给每一行加上行号
| grep "\\[id=.*\\]" #打印所有的`id`配置行
| grep -A 1 "\\[id=$userID\\]" #打印匹配的ID行,及下一行,下一行即为下一个配置section的起始行
| awk '{print $1}' | xargs #提取两个行号,即所需section的起始行和下一个配置section的起始行。

这种方法当时也是突发其想,想出来的。现在回头看看,其实使用倒推法应该不难想出这种方法。即最后应该是使用sed处理指定区间的文本。那前提就是需要找出section的区间了。而区间也就是两个行号,自然想到要cat -n了。

shell 读配置文件的更多相关文章

  1. Shell读取配置文件的方法

    参考:http://www.cnblogs.com/binbinjx/p/5680214.html 做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参 ...

  2. java工具类-读配置文件

    ///读配置文件 import java.io.InputStream;import java.util.HashMap;import java.util.Map;import java.util.M ...

  3. shell的配置文件

    1. bash shell 的配置文件 bash shell的配置文件很多,可以分成下面类别 1.1 按生效范围划分两类 全局配置:针对所有用户皆有效 /etc/profile /etc/profil ...

  4. linux bash shell 的配置文件

    按生效范围划分两类 全局配置:针对所有用户皆有效 /etc/profile /etc/profile.d/*.sh /etc/bashrc 个人配置:只针对特定用户有效 ~/.bash_profile ...

  5. linux shell的配置文件执行顺序

    shell配置文件的作用:初始化环境变量.设置命令提示符.指定系统命令路径等 shell配置文件分类: (1)系统级别配置文件: /etc下,比如/etc/profile./etc/bashrc (2 ...

  6. Java Properties 类读配置文件保持顺序

    前几天,公司项目中有一个需求是读取配置文件的,而且最好能够保证加载到内存中的顺序能够和配置文件中的顺序一致,但是,如果使用 jdk 中提供的 Properties 类的话,读取配置文件后,加载到内存中 ...

  7. shell 读取配置文件的方法

    原文地址:http://bbs.chinaunix.net/thread-3628456-1-1.html 总结地址:https://www.cnblogs.com/binbinjx/p/568021 ...

  8. [BUG]读配置文件中文, 查询不到数据库

    配置文件编码, 要和数据库编码一致

  9. 读配置文件property文件

    import java.io.IOException;import java.util.Properties; import org.springframework.core.io.support.P ...

随机推荐

  1. [BZOJ2440]完全平方数解题报告|莫比乌斯函数的应用

    完全平方数 小 X 自幼就很喜欢数.但奇怪的是,他十分讨厌完全平方数.他觉得这些数看起来很令人难受.由此,他也讨厌所有是完全平方数的正整数倍的数.然而这丝毫不影响他对其他数的热爱.  这天是小X的生日 ...

  2. bzoj 1093 缩点+DP

    首先比较明显的是如果存在一个半连通子图,我们将其中的环缩成点,那么该图仍为半连通子图,这样我们就可以先将整张图缩点,重新构图,新图为拓扑图,记录每个新的点表示的强连通分量中点的个数num[i],那么我 ...

  3. feign hystrix 线程池伸缩控制

    当前使用的版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spr ...

  4. php中的base64写shell

    <?php system(base64_decode($_GET['info'])); #http://localhost/1.php?info=d2hvYW1p #这只是一个例子 ?>

  5. Linux-进程间通信(四): 域套接字

    1. 域套接字: (1) 只能用于同一设备上不同进程之间的通信: (2) 效率高于网络套接字.域套接字仅仅是复制数据,并不走协议栈: (3) 可靠,全双工: 2. 域套接字地址结构: struct s ...

  6. Python Matplotlib图表汉字显示成框框的解决办法

    http://blog.sina.com.cn/s/blog_662dcb820102vu3d.html http://blog.csdn.net/fyuanfena/article/details/ ...

  7. Python 如何将字符串转为字典

    在工作中遇到一个小问题,需要将一个 python 的字符串转为字典,比如字符串: user_info = '{"name" : "john", "ge ...

  8. sicily 1020. Big Integer

    Description Long long ago, there was a super computer that could deal with VeryLongIntegers(no VeryL ...

  9. 【bzoj2006】NOI2010超级钢琴

    补了下前置技能…… 题意就是求一段区间的权值和前k大的子序列的和. 把段扔进优先队列 每次拿出来之后按照所选择的j进行分裂 #include<bits/stdc++.h> #define ...

  10. 2017多校第6场 HDU 6097 Mindis 计算几何,圆的反演

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6097 题意:有一个圆心在原点的圆,给定圆的半径,给定P.Q两点坐标(PO=QO,P.Q不在圆外),取圆 ...