View Single Post
Go4Expert Member
1Jul2007,21:58  
munkyeetr's Avatar
Okay, have you tried running your code? Does it do what you need?

I have a few words of advice:

1) You do not need to declare so many variables. Variables generally hold values that change, hence the word variable. So, in our script, the only value that really changes is the User ID. For each different User ID we perform the same actions. We can virtually eliminate all the variables you have declared, except one. I'll give you a hint: keep the $uid variable.

2) System commands do not need to be declared as variables. They can just be called from the script. That is what a script is; a series of system commands. So things like groupadd and useradd can just be called. HINT: you may need to set the path to your user commands depending on the distibution you are using. For instance in Fedora 7 (which I am using) I need to be specific about calling /usr/sbin/useradd. Just calling useradd gives me an error command not found.

3) The biggest problem I see with your code is that you are trying to manipulate the /etc/passwd file directly. THIS IS NOT A GOOD IDEA.
The useradd, usermod, and userdel commands will handle all this for you, and they will edit the file correctly. Use the tools in the toolbox and don't try to reinvent the wheel. Letting these system commands do the work for you will also cut down your code substantially (The script I have written is only 8 lines long).

You are on the right track. Your use of the while loop is correct, though the contents of it are not (again, do not manipulate the /etc/passwd file directly).

Give it another try, taking into account the advice I've given here.