Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2001, 09:30 AM
Jessman72 Jessman72 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Centreville, Alabama
Posts: 25 Jessman72 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 3 sec
Reputation Power: 0
Send a message via AIM to Jessman72
Talking

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);

Reply With Quote
  #2  
Old April 13th, 2001, 11:33 AM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 24 sec
Reputation Power: 9
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;

Reply With Quote
  #3  
Old April 13th, 2001, 04:37 PM
Jessman72 Jessman72 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Centreville, Alabama
Posts: 25 Jessman72 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 1 m 3 sec
Reputation Power: 0
Send a message via AIM to Jessman72
Thanks!!

Thanks....that did the trick!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > Change a value in a file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway