加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

读取自定义配置文件

发布时间:2020-12-15 23:49:30 所属栏目:大数据 来源:网络整理
导读:配置文件样例 # more test.conf? ### Database Configration #### host = 10.8.9.20; dbname = webdb ; user = admin ; pwd = admin; 2.测试程序内容 #!/usr/bin/perl use strict; our($host,$dbname,$user,$password); my $prog_root="/usr/test_prog"; my
  1. 配置文件样例


# more test.conf?

### Database Configration ####

host = 10.8.9.20;

dbname = webdb ;

user = admin ;

pwd = admin;




2.测试程序内容


#!/usr/bin/perl

use strict;

our($host,$dbname,$user,$password);

my $prog_root="/usr/test_prog";

my $prog_conf = $prog_root."/test.conf";


open my $conf_file,"<",$prog_conf;

my @conf_items = <$conf_file>;

close($conf_file);


foreach my $line(@conf_items){

? ?#### read each line from configration file

? ?$host = $1 if ($line =~/hosts*=s*(S+)s*;/i);

? ?$dbname=$1 if ($line =~/dbnames*=s*(S+)s*;/i);

? ?$user=$1 if ($line =~/users*=s*(S+)s*;/i);

? ?$password=$1 if ($line =~/pwds*=s*(S+)s*;/i);


}

print "$hostn";

print "$dbnamen";

print "$usern";

print "$passwordn";


3.运行输出结果


# perl conf.pl?

10.8.9.20

webdb

admin

admin

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读