|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
I am trying to write a script that looks a password file and changes the password for the username given. However, when the script runs it will not change the password. I can change it to print out the line it is finding, but it prints all lines...Could someone please offer an idea on how to change only the password value.
The entries in the password file follow this format: first|last|username|password Code: #!/usr/bin/perl print "Enter username:\n"; $username=<STDIN>; chomp($username); print "Please enter Your new password"; $word=<STDIN>; chomp($word); $file="/tmp/pass.db"; open PASS, $file or die "Cannot open $file for changes :$!"; @list=<PASS>; foreach $i (@list) { $line="$i"; @entry = split(/\|/,$line); if ($entry[2] == "$username") { $entry[3]="$word"; } } close(PASS); |
|
#2
|
||||
|
||||
|
The problem is that you're only changing the value in the array and not writing the changes back out to the file. This should work:
-------------------------------------------------------------- #!/usr/bin/perl for (;;) { print "Enter username:\n"; chomp($username=<STDIN>); last if $username =~ m{\S+}; } for (;;) { print "Please enter Your new password"; chomp($word=<STDIN>); last if $word =~ m{\S+}; } $file="/tmp/pass.db"; open PASSIN, "<$file" or die "Cannot open $file for changes :$!"; open PASSOUT ">$file.new" or die "Cannot open new file: $!\n"; while (<PASSIN>) { chomp; @entry = split /\|/; $entry[3] = $word if $entry[2] eq $username; print PASSOUT join('|',@entry), "\n"; } close(PASSIN); close(PASSOUT); rename $file, "$file.bak"; rename "$file.new", $file; |
|
#3
|
|||
|
|||
|
Thanks!!
Thanks....that did the trick!
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Perl Programming > Change a value in a file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|