It began, while I was trying to read in, modify and write out shadow file entries:
open(TEMPFILE, ">${TempPath}/temp_shad") || die; open(SHADFILE, "${TempPath}/$passwdfile") || die; while (<SHADFILE>) { chomp; (@entry) = split(/:/,$_); $newentry = join(":", @entry); print TEMPFILE "$newentry\n";
This didn't work, because, as you know (if you are a *nix SA), the shadow file looks like so: foo:asDflkj123./d:6445::::::
So when I wrote it back out,this is what I got:
foo:asDflkj123./d:6445
In comes the concept of slicing <*trumpet sounds*>. Thru the slice, I was able to define those empty fields, while not having to worry about assigning bogus values that I would have to delete. Also, I didn't have to deal w/ trying to assign each field to an individually defined variable and the long lines of code that followed:
The above code works, but geez, I don't want to have to type all of that each time I need to modify the shadow file.($user,$passwd,$mod,$f1,$f2,$f3,$f4,$f5) = split(/:/,$_); $newentry = "$user" . ":" . "$passwd" . ":" . "$mod" . ":" . "$f1" . " +:" . "$f2" . ":" . "$f3" . ":" . "$f4" . ":" . "$f5)\n";
That code became this:
In short, it made a potentially arduous task, very simple and easier to read.(@entry[0..8]) = split(/:/,$_); $newentry = join(":", @entry[0..8]);
Now, back to the regularly scheduled program...
Enlightened,
bb
update: fixed # of colons in shadow file entry, thanks davorg
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: I am now slice-aware
by davorg (Chancellor) on Apr 17, 2001 at 18:46 UTC | |
by tadman (Prior) on Apr 17, 2001 at 19:15 UTC | |
by davorg (Chancellor) on Apr 17, 2001 at 19:19 UTC | |
by jeroenes (Priest) on Apr 17, 2001 at 19:22 UTC | |
by birdbrane (Chaplain) on Apr 17, 2001 at 19:29 UTC | |
by davorg (Chancellor) on Apr 17, 2001 at 19:33 UTC | |
by birdbrane (Chaplain) on Apr 17, 2001 at 19:44 UTC | |
|
Re: I am now slice-aware
by frankus (Priest) on Apr 17, 2001 at 18:52 UTC | |
by davorg (Chancellor) on Apr 17, 2001 at 19:00 UTC | |
by frankus (Priest) on Apr 17, 2001 at 19:10 UTC | |
by birdbrane (Chaplain) on Apr 17, 2001 at 19:19 UTC |