Operating System - HP-UX
1760854 Members
3928 Online
108895 Solutions
New Discussion юеВ

Need help - script for adding users

 
SOLVED
Go to solution
Angie_1
Regular Advisor

Need help - script for adding users

I need some help writing a script to add users.
I am very new to writing scripts and have read a bit on case statements and half way understand them.
So I would prefer it to be written with the case statement if that can be done.
Can someone please help me?

Thx..Angie
12 REPLIES 12
Angie_1
Regular Advisor

Re: Need help - script for adding users

I wanted to add that if the "if" and "then" can be done I somewhat understand that too. I am trying my best to learn but still need some help.

Thx..Angie
Sridhar Bhaskarla
Honored Contributor

Re: Need help - script for adding users

Hi,

You will need to input the information about the logins somehow. Let's say you have an input file in this format

#cat user_input

user1 - 5000 - User1, some info - 20 - /usr/bin/ksh
user2 - 5001 - User2, some info - 20 - /sbin/sh
user3 - 5002 - User3, some info - 20 - /sbin/csh

You can write a very simple script like this

#!/usr/bin/ksh

cat user_input |while read line
do
USER=$(echo $line|awk '{FS="-";print $1}')
UID=$(echo $line|awk '{FS="-";print $2}')
GECOS=$(echo $line|awk '{FS="-";print $3}')
GID=$(echo $line|awk '{FS="-";print $4}')
SHL=$(echo $line|awk '{FS="-";print $5}')
echo "Adding $USER"
useradd -u $UID -g $GID -s $SHL -c "$GECOS" -m -k /etc/skel $USER
passwd -df $USER
done

The above will force reset the passwords of the users so that when then login, it will prompt to chage their passwords.

It is a security risk. You can generate a an encrypted password and enter it into passwd file. You can search in the forums and find the ways of doing it. There are quite a number of threads on it.

I hope it can help you to start with.
-Sri





You may be disappointed if you fail, but you are doomed if you don't try
Angie_1
Regular Advisor

Re: Need help - script for adding users

I guess I should have explained a little bit better.
This will be given to the help desk, this script. A new user will be setup by the helpdesk maybe once a week.

They need to then have a menu and choose from the menu the 'add user' or 'change user' or 'delete user'... options.

Then they will input the information at the various prompts about who they are adding (for example).
It should ask too what group they will be in (sales, engineering) and if they are in one of those groups, then it should use the group ID assigned to that group (like 20 or 30 or whatever).

I wonder if Case statements or If/then would work? I know nothing about awk so I wanted to avoid that. Please help...thx.Angie
James R. Ferguson
Acclaimed Contributor
Solution

Re: Need help - script for adding users

Hi Angie:

As a general rule, 'case' statements are superior to 'if/else/if...' oscillations for three or more cases.

The code is generally faster to execute, smaller, and perhaps most importantly, easier for the human to read and maintain.

Regards!

...JRF...
A. Clay Stephenson
Acclaimed Contributor

Re: Need help - script for adding users

If that's all you are doing then the better answer is to set up a restricted SAM session to allow your help-desk operators to add users under SAM. That is likely to be a better user interface than you are going to be able to do on your own.

Man sam for details and note the "Restricted SAM" section.
If it ain't broke, I can fix that.
Chris Lonergan
Advisor

Re: Need help - script for adding users

Have you thought about using restricted SAM. That way you could set up a login that went straight into a SAM session. This SAM session could be restricted to creating and manipulating users and groups.

If you want to learn more about case and if statements try getting a copy of "Learning the Korn Shell" by O'Reilly.

Chris
Angie_1
Regular Advisor

Re: Need help - script for adding users

Ok I could use Restrictred Sam..that is a great idea.
Otherwise do you all agree that Case statements are better for something like this? And if it is better, please explain to me why.

Thanks..Angie
A. Clay Stephenson
Acclaimed Contributor

Re: Need help - script for adding users

Better is a tougher question. The case MIGHT be less efficient but will almost always be much more readable. The problem with many if's is that they rapidly become clumsy. In general, if it's a simple if then else then if's are probably your best choice but if the branches are above 3 then the case is usually the better weapon.

In almost all cases, readability is valued above pure efficiency.
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Need help - script for adding users

Hi,

Restricted SAM is a great idea.

I would not say "case" is the better statement than if-then-else as it is dependent on the function of the script, but it is the famous statement used in cases like this. If you are willing to write a script, then I would suggest that you write functions for each action and call them from the case statement. If there are a lot of cases, if-then-else can confuse you enough.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try