in reply to So you know Perl; but do you know programming?

Why bother with the rewrite? It wasn't broken, so there wasn't much to gain, not even in running time.

Furthermore, there's a lot more that you could have improved. Why are you parsing a password file yourself? That's why you have getpwent. And what's the point of storing all the existing usernames in a hash? A single call with getpwnam could have told whether a name was free or not.

Finally, this program seems to favour reusing userids over issueing unused ones. Reusing userids is not considered a good thing - a new user might inherit files and other pieces of history from a previous user. It's better to just pass over all the entries, remember the highest issued id, and use the next one available.

And if you really wanted to impress your boss, you would have replaced the program with a one liner:

exec useradd => @ARGV
Or, if you are on Linux, and really want a question and answer session:
exec 'adduser'
Of course, I would have replaced it with a 0 liner, and used the already available tools.

Abigail

Replies are listed 'Best First'.
Re^2: So you know Perl; but do you know programming?
by Aristotle (Chancellor) on Jul 04, 2002 at 14:47 UTC

    Whether or not to use an own tool over the existing ones is a matter of several factors. Though I have to agree that useradd is the better idea, using solely existing tools is a question of purpose and may or may not be justified in this case. Marza's script may be doing more than we see from the snippet.

    F.ex, I wrote a script for a site I worked at that dealt with the details of calling useradd for me; the same script also did a bunch of other things like add entries to a URL rewrite map, an ftpd configuration and several other steps.

    Makeshifts last the longest.

      Correct. It does a few more things as I told Abigail. They want an all in one script to handle all accounts in one run. Automation of a hetrogenus environment. Hey where is SSO? ;)

Re: Re: So you know Perl; but do you know programming?
by Marza (Vicar) on Jul 04, 2002 at 17:59 UTC

    Hello Abigail!

    The answer is simple. The script is used because we have NIS. Useradd only supports local entries.

    The snippet was only an example of doing much more than was needed: 7 loops and three file opens to simply verify a username and to get a uid?

    Sure the hashing is overkill. At the time I either didn't know about getpwnam or forgot about it. However, even with the 2 hashes. It reduced the looping to 2 and 1 file open. However, with the suggestions. I don't plan to use the hashes and I am dropping 1 loop.

    As to reusing uids? You are correct. However, when an employee leaves, his account is disabled(ie "**" for a password. A list of stuff owned is generated and other workers go through his files and chown what is needed and delete what is not. This eliminates "We forgot what it was for so we should hold onto it." When there are no more files owned, the account gets deleted.

    Finally, the script does more then what the snippet shown. Mail entries and mail lists, Meeting Maker account creation(what will be added), and a section for NT creation as well as other stuff like config files, etc.

    But I do thank you for the comments!