读取配置文件的shell脚本实现
来源:互联网 发布:浪潮软件免费下载 编辑:程序博客网 时间:2024/06/04 20:07
(本人正在参加2015博客之星评选,诚邀你来投票,谢谢:http://vote.blog.csdn.net/blogstar2015/candidate?username=zhouzxi)
最近,在从事开发工作的过程中,遇到了一个需求:用shell脚本实现对配置文件的读取,并将读取到的配置项写入到新的文件中。
为了完成该需求,我们考虑使用awk命令,该命令把文件中的内容逐行的读入,按照特定的规则对每行记录进行处理。
例如,我们要从文件Config.ini(位于当前用户的etc目录下)中获取各个配置项的值,并将之写入到NetworkInfo.txt文件中。其中,Config.ini文件的内容如下:
[network]; ip of databaseip=10.10.10.10; port of databaseport=9999
我们编写如下的shell脚本GetDatabaseConfig.sh:
#!/bin/kshCFG_FILE=$HOME/etc/Config.iniIP=` awk '$1~/\[.*/{_cdr_par_=0}\ $0 ~ /^ *\[ *network *\]/ {_cdr_par_=1}\ $0~/^[\011 ]*ip *=.*/ { if(_cdr_par_==1) { sub("="," "); print $2; exit 0} }\ ' ${CFG_FILE}`PORT=` awk '$1~/\[.*/{_cdr_par_=0}\ $0 ~ /^ *\[ *network *\]/ {_cdr_par_=1}\ $0~/^[\011 ]*port *=.*/ { if(_cdr_par_==1) { sub("="," "); print $2; exit 0} }\ ' ${CFG_FILE}`main(){ echo "ip =" $IP echo "port =" $PORT echo "[network]" >> NetworkInfo.txt echo "ip=$IP" >> NetworkInfo.txt echo "port=$PORT" >> NetworkInfo.txt echo "finish !!!!!!!!!!!"}## Execute main functionmain $*
将该脚本上传到Linux机器上,并使用“chmod 777 GetDatabaseConfig.sh”修改该脚本的权限,然后运行“./GetDatabaseConfig.sh”命令,结果如下:
ip = 10.10.10.10port = 9999finish !!!!!!!!!!!
同时,可以看到在当前目录下生成了NetworkInfo.txt文件,其内容为:
[network]ip=10.10.10.10port=9999
本文中的shell脚本已提交到GitHub上,欢迎下载阅读:https://github.com/zhouzxi/GetDatabaseConfig
参考资料:
1.http://www.cnblogs.com/ggjucheng/archive/2013/01/13/2858470.html
2.http://www.cnblogs.com/repository/archive/2011/05/13/2045927.html
4 1
- 读取配置文件的shell脚本实现
- shell脚本读取配置文件
- shell脚本读取配置文件
- shell脚本读取配置文件
- shell脚本读取ini配置文件的值
- shell脚本读取ini配置文件的值
- shell脚本读取ini配置文件的值
- 如何用shell脚本读取配置文件
- Shell脚本读取ini配置文件实例教程
- linux shell 脚本读取 ini 配置文件
- Shell实现读取ini格式配置文件方法
- Linux shell读取配置文件使脚本更灵活(刚接触,求更好的办法)
- shell脚本按行读取配置文件并解析
- login shell 的配置文件读取流程
- shell 读取配置文件
- shell读取配置文件
- linux shell 读取配置文件
- linux shell 读取配置文件
- 绘图 - 7
- 关联容器(六):unordered_set
- GridControl的模糊查询
- 单例模式
- 俄罗斯方块
- 读取配置文件的shell脚本实现
- oracle 随机获取表中记录
- OC-031.类扩展
- hdu1272小希的迷宫
- 自定义label高度
- 截取与分析日志文件的特定行数的操作
- linux中mount命令参数详解(nfs 参数)
- 关联容器(七):unordered_multiset
- windows上使用notepad++编辑shell脚本,并通过cygwin运行