码迷,mamicode.com
首页 > 系统相关 > 详细

Shell读取配置文件的方法

时间:2017-04-13 18:11:04      阅读:308      评论:0      收藏:0      [点我收藏+]

标签:文件   http   方法   read   htm   ash   lin   html   批量   

参考:http://www.cnblogs.com/binbinjx/p/5680214.html

做批量软件安装自动化时,都喜欢用配置文件的方式改变参数,那怎么通过shell读取配置文件的配置呢?参考以上链接,根据易用性依次讨论三种方法:

假设配置文件config的内容如下:

#!/bin/bash
#configuration
ID=123
IP=192.168.3.154
Name=test

1 直接将配置信息加载到session的环境变量中

#source config

#echo $ID
123
#echo $IP
192.168.3.154
#echo $Name
test

2 利用eval方法解析

while read line;do  
    eval "$line"  
done < config  
echo $ID  
echo $IP  
echo $Name  

3 利用sed解析文本,提取配置信息

id=`sed /^ID=/!d;s/.*=// urfile`  
ip=`sed /^IP=/!d;s/.*=// urfile`  
name=`sed /^Name=/!d;s/.*=// urfile`  
echo $id  
echo $ip  
echo $name 

 

Shell读取配置文件的方法

标签:文件   http   方法   read   htm   ash   lin   html   批量   

原文地址:http://www.cnblogs.com/lizhaoxian/p/6704916.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!