Script help

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi !
I have the Idea to make a perl script (better than in bash) to make some configuration on linux.
I think to have some files like param.txt with some lines like:
param_one = ZZZ XX CCCC VV
param_two = ZZZZZZZZZ
param_three = X

so need to check if param_one is in some_file.conf and if it is there test the value .. if it is not .. add to some_file.conf ... I think it should be easy ... right ??

Thanks for any help ..


Re: Script help

On 30/12/2014 05:49, wrote:
Quoted text here. Click to load it

create the files


    p1 = AA
    p2 = aa
    p3 = AC


    p2 = aa
    p3 = BB
    p4 = BC


    p2 = BA
    p4 = BB
    p6 = CA

and run the following code for fun !

use strict;
use warnings;
use feature qw/say/;
my %DB;

read_properties('param1.txt', 'param2.txt', 'param3.txt');

say query_file('p1');
say query_file('p2');
say query_file('p3');
say query_file('p4');
say query_value('p1');
say query_value('p2');
say query_value('p3');

sub read_properties {
foreach my $file (@_) {
open FILE, '<', $file or die $!;

    while (<FILE>) {
    my ($key,$val) = $_ =~ /^\s*(.*?)\s*=\s*(.*?)\s*$/ or next;
    push @->}, $file;
    $DB ->->=1
close FILE}

sub query_file  
sub query_value
sub query       {
(my $who = ((caller 1)[3]))=~s/^.*:://;

    if ($who eq 'query_file') {
    exists $DB-> ?
    return join ', ', @->} :
    return 'NOT_EXIST_IN_ANY_FILE'
    elsif ($who eq 'query_value') {
    exists $DB ?
    return join ', ', keys % ->} :
    else {
    die "Method $who does not exist\n"

Re: Script help

On 30/12/14 03:49, wrote:
Quoted text here. Click to load it

1. It's not hard.  Grab lines from the first file, use a hash to store  
them, with "param_one" etc as keys.  Then run through the second file in  
like manner, doing stuff.

2. But is it necessary?  You maintain param.txt rather than  
some_file.conf, which is much the same task in terms of effort, with the  
added disadvantage that you now have two places you can make changes.  
And in fact, since lots of Unix utilities have a cascading set of  
configuration files, there's even more room for confusion.

NB: Don't discuss point 2 any more here; it's way off topic.


Henry Law            Manchester, England

Site Timeline