hello,
I've script who allow me to create a file "X" with data storing in a array(the array is in my script).
If the file "X" existing,my script take the variable,values in this existing file to create another file.
when i start my script, i ask (prompt) the user to write the values of the variables and for some variables it's not necessary, so this varibale are autovalidated.
So i've make a condition for this variables and my condition don't run.
firstly i've succeded to run my script by putting white space( " " ) in the array for this variable who need not to be seized;
but if the file existing,i take the variable and values from the file and not the array, so the variable who must be autovalidated are no values in the file,and when i run the script the variable are not autovalidated;the script say me to write a values for this.
my script :
the part where i test if the variable must be autovalidated or not
when i run my script, if i've not a file (so i take the value from the array), i've this
the script create this file
and when i run another times my script(he take the values from the file i've created above), i've this
he want i take a value for var5 and i don't understand why ?
thanks
I've script who allow me to create a file "X" with data storing in a array(the array is in my script).
If the file "X" existing,my script take the variable,values in this existing file to create another file.
when i start my script, i ask (prompt) the user to write the values of the variables and for some variables it's not necessary, so this varibale are autovalidated.
So i've make a condition for this variables and my condition don't run.
firstly i've succeded to run my script by putting white space( " " ) in the array for this variable who need not to be seized;
but if the file existing,i take the variable and values from the file and not the array, so the variable who must be autovalidated are no values in the file,and when i run the script the variable are not autovalidated;the script say me to write a values for this.
my script :
Code:
#!/usr/bin/perl -w############################################################################### use strict;use warnings;use English;use Getopt::Long; use POSIX qw(strftime); ############################################################################### my $sansdemande;my $tablo; my @tab = ( { "NAME" => "var1", "VALUE" => "", "DESC" => "test1"}, {"NAME" => "var2", "VALUE" => "", "DESC" => "test2"}, {"NAME" => "var3", "VALUE" => "", "DESC" => "test3"}, {"NAME" => "var4", "VALUE" => "", "DESC" => "test4"}, {"NAME" => "var5", "VALUE" => " ", "DESC" => "test5"}, ); my $result = GetOptions ("sansdemande" => $sansdemande, "tablo" => $tablo, ); sub fonc{my $validation;$validation="n";my $ancienne;my $new;my $validated="novalide"; if (length($_[0])>0){ $validation = "o"; $ancienne = $_[0]; } if ($_[1] eq "var3" or $_[1] eq "var5" ){$validated = "v";} if ($validation ne "o" ){ if($sansdemande) { print $_[1]." [".$_[0]."] : <auto validation>\n\n"; } else { do { print $_[1]." [".$_[0]."] : "; $_[0] = <stdin>; print "\n"; chomp($_[0]); } while (! ($_[0])); }} elsif ($validated eq "v" ) { print $_[1]." [".$_[0]."] : <auto validation>\n\n"; $validated = "novalide";}else{ if($sansdemande) { print $_[1]." [".$_[0]."] : <auto validation>\n\n"; } else { print $_[1]." [".$_[0]."] : "; $_[0] = <stdin>; $new = $_[0]; if ((length($new)-1) == 0) { $_[0] = $ancienne; } print "\n";chomp($_[0]); }} } ################################################################################ my $madate= strftime('%Y%m%d%H%M%S', localtime);my $filename = 'fic.txt';my $filename2;my $bool = 'false';my $bool2 = 'false';my @values1 = ();my $com2;my $var;my $var2;my $var1;my $var3; if (-f $filename){ $bool = 'true'; my $infile = $filename; open DATA, $infile or die $!;foreach my $intext ( <DATA> ) { if ($intext =~ m/^s*#\w+/ and $intext !~ m/^\s*#-\s*/) { $com2 = $com2."\n".$intext; chomp($com2); } if ( $intext !~ m/^s*#/ and $intext !~ m/^\s*$/ and $intext =~ m/=/ ) { my ($name, $value) = split /s*=s*/, $intext; chomp($name); chomp($value); $name =~ s/^s*exports+(w+)/$1/; push (@values1,[$name,$value,$com2]); $com2=""; } } $filename2 = $filename.$madate;rename($filename,$filename2); } if($tablo){ foreach $var (@tab) { &fonc($var->{'VALUE'},$var->{'NAME'},$var->{'DESC'}); } }else{ if ($bool eq "true" ) { print "\nPRENDRE LES VALEURS DANS LE FICHIER PROFILE\n"; $bool2 = 'true'; for ($var2 = 0; $var2 < scalar @values1; ++$var2) { foreach $var (@tab) { if($var->{'NAME'} eq $values1[$var2][0]) { &fonc($values1[$var2][1] , $values1[$var2][0] , $values1[$var2][2] ); } } } } else { foreach $var (@tab) { &fonc($var->{'VALUE'},$var->{'NAME'},$var->{'DESC'}); } } } open(FILE1, '>', $filename) || die("impossible d'ouvrir $filename en écriture" ); my $max;if ( $bool2 eq 'true') { for ($var3 = 0; $var3 < scalar @values1; ++$var3) { if($values1[$var3][0] eq "var2" ) { $max = $values1[$var3][1]; } print FILE1 "#"; print FILE1 "-"x78; print FILE1 $values1[$var3][2]."\n"; print $values1[$var3][2]."\n"; print FILE1 "#"; print FILE1 "-"x78; print FILE1 "\n"; if($values1[$var3][0] eq "var3" ) { $values1[$var3][1] = $max*969; } print FILE1 "export ".$values1[$var3][0]."=".$values1[$var3][1]."\n\n"; } }else{ foreach $var1 (@tab) { if($var1->{'NAME'} eq "var2" ) { $max = $var1->{'VALUE'}; } print FILE1 "#"; print FILE1 "-"x78; print FILE1 "\n"; print FILE1 "#".$var1->{'DESC'}."\n"; print FILE1 "#"; print FILE1 "-"x78; print FILE1 "\n"; if($var1->{'NAME'} eq "var3" ) { $var1->{'VALUE'} = $max*969; } print FILE1 "export $var1->{'NAME'}=$var1->{'VALUE'}\n\n"; }} close(FILE1);#
the part where i test if the variable must be autovalidated or not
Code:
if ($_[1] eq "var3" or $_[1] eq "var5" ){$validated = "v";}
when i run my script, if i've not a file (so i take the value from the array), i've this
var1 [] : essa
var2 [] : 12
var3 [] :
var3 [] : s
var4 [] : ep
var5 [ ] : <auto validation>
the script create this file
#------------------------------------------------------------------------------
#test1
#------------------------------------------------------------------------------
export var1=essa
#------------------------------------------------------------------------------
#test2
#------------------------------------------------------------------------------
export var2=12
#------------------------------------------------------------------------------
#test3
#------------------------------------------------------------------------------
export var3=11628
#------------------------------------------------------------------------------
#test4
#------------------------------------------------------------------------------
export var4=ep
#------------------------------------------------------------------------------
#test5
#------------------------------------------------------------------------------
export var5=
and when i run another times my script(he take the values from the file i've created above), i've this
var1 [essa] :
var2 [12] :
var3 [11628] : <auto validation>
var4 [ep] :
var5 [] :
he want i take a value for var5 and i don't understand why ?
thanks