hello,
i've script who write in a file values they are stored in a array.
If the file existe, he take the variables and values from the files.
If i had a new variable in my file, he create me a new file with the variable are stored in the existing file but i've not the new variable i've add in the file.
For example
if i've no existing file the script create this file
if i modify the file and change the password from xxxx to tonnypass, if i run the script and change nothing i've this
but if i want to add a new value like this
and if i run my script for modify the login,it create me a new file but i lose the variable i've created, i've this
This is my script
thanks
i've script who write in a file values they are stored in a array.
If the file existe, he take the variables and values from the files.
If i had a new variable in my file, he create me a new file with the variable are stored in the existing file but i've not the new variable i've add in the file.
For example
if i've no existing file the script create this file
#Login
export Login=tonny
#PAss connexion
export Password=xxxx
if i modify the file and change the password from xxxx to tonnypass, if i run the script and change nothing i've this
#Login
export Login=tonny
#PAss connexion
export Password=tonnypass
but if i want to add a new value like this
#Login
export Login=tonny
#PAss connexion
export Password=tonnypass
#port
export port=28
and if i run my script for modify the login,it create me a new file but i lose the variable i've created, i've this
i don't understand ??#Login
export Login=benny
#PAss connexion
export Password=tonnypass
This is my script
Code:
#!/usr/bin/perl -w
use strict;
use warnings;
use English;
use Getopt::Long;
use POSIX qw(strftime);
###############################################################################
my $re = 0;
my $des = 0;
my $filename = 'confus.txt';
my $infile = $filename;
my $madate = strftime( '_%Y%m%d%H%M%S', localtime );
my $exist_file = 'false';
my $var;
my $com;
###############################################################################
my @connexion = (
{ "NAME" => "Login",
"DEFAULTVALUE" => "tonny",
"VALUE" => "",
"DEFAULTDESC" => "Login",
"DESC" => "",
"AUTOVALIDATED" => "false"
},
{"NAME" => "Password",
"DEFAULTVALUE" => "xxxx",
"VALUE" => "",
"DEFAULTDESC" => "PAss connexion ",
"DESC" => "",
"AUTOVALIDATED" => "false"},
);
###############################################################################
my %vari_name;
foreach (@connexion) {
$vari_name{ $_->{'NAME'} } = $_;
}
################################################################################
sub check {
my $var_value = shift;
my $var_name = shift;
my $ancienne = $var_value;
my $new;
if ($des) {
print $vari_name{$var_name}{"DESC"} . " :\n";
}
if ( length($var_value) == 0 ) {
do {
print $var_name . " [" . $var_value . "] : ";
$var_value = <stdin>;
print "\n";
chomp($var_value);
} while ( length($var_value) == 0 );
}
else {
print $var_name . " [" . $var_value . "] : ";
$var_value = <stdin>;
$new = $var_value;
if ( ( length($new) - 1 ) == 0 ) {
$var_value = $ancienne;
}
print "\n";
chomp($var_value);
}
return $var_value;
}
###############################################################################
my $result = GetOptions(
"res" => \$re,
"des" => \$des
);
if ( $result == 0 ) { exit 1; }
if ( -f $infile ) {
$exist_file = 'true';
open DATA, $infile or die $!;
foreach my $intext (<DATA>) {
if ($intext =~ m/^\s*#\w+/ and $intext !~ m/^\s*#-\s*/)
{
$com = $com."\n".$intext;
chomp($com);
}
my ( $name, $value ) = split /\s*=\s*/, $intext;
chomp($name);
chomp($value);
$name =~ s/^\s*export\s+(\w+)/$1/;
$vari_name{$name}{"VALUE"} = $value;
$vari_name{$name}{"DESC"} = $com;
$com="";
}
}
rename( $infile, $infile . $madate );
#}
if ( $re or ( $exist_file eq "false" ) ) {
foreach $var (@connexion) {
$var->{"VALUE"} = $var->{"DEFAULTVALUE"};
$var->{"DESC"} = $var->{"DEFAULTDESC"};
}
}
foreach $var (@connexion) {
$var->{"VALUE"} = &check( $var->{"VALUE"}, $var->{"NAME"} );
}
open( FILE, '>', $infile )
or die("impossible open $filename ");
foreach $var (@connexion) {
print FILE "#" . $var->{'DESC'} . "\n";
print FILE "export $var->{'NAME'}=$var->{'VALUE'}\n\n";
}
close(FILE);
thanks