1826399 Members
4141 Online
109692 Solutions
New Discussion

Re: useradd script help

 
SOLVED
Go to solution
Tapas Jha
Valued Contributor

useradd script help

Hi,

I want to write one useradd shell script which will customised useradd command.
My script also ask for ftp user, cron user setup and according to it will
add ftpuser, cron user in respective files.

Also i want to write script in such a way that options will be in any order,
i mean, like below:

useradd.sh -g -s -c "comment" -d -m login

Or
useradd.sh -s -u -d -c "comment" -g login
or
useradd login

The missing options will be the default settings.

How can i write in my script that which argument for , which is
for , which is for , which is for ...bla bla bla?<BR /><BR />Please assist.<BR /><BR />With regards<BR />Tapas
Tapas Jha
5 REPLIES 5
Jeroen Peereboom
Honored Contributor

Re: useradd script help

Tapas,

Check the manpage of getopts.
It contains an interesting example too.

JP
Bart Paulusse
Respected Contributor

Re: useradd script help

Hi Tapas,

you will have to test your input parameters.
eg. if %1 is -s then %2 is shell. You hae to do this for all your input parameters.
There are a lot of different ways in which you can do this.
The if methed may be easiest to understand but it will require a lot of if statements.
Using another method like "case" would be better but everyone's got different preferences when scripting.

regards
Gary L. Paveza, Jr.
Trusted Contributor
Solution

Re: useradd script help

Here's one that I just wrote to do this. In addition to what you wanted, if a field is not specified on the command line it prompts for it.

Hope it helps.

Gary
Dani Seely
Valued Contributor

Re: useradd script help

Hey Tapas,
Need more information. Are you wanting this script to be interactive or did you want the user to just type all of the info on a command line?

If you are looking for an interactive script, you could use something like this, but you would have to provide some error checking to ensure a valid shell is entered, a uid or gid is not being duplicated, etc.

echo "\nPlease enter username:"
read username
echo "\nPlease enter user id: "
read userid
echo "\nPlease enter group: "
read group
echo "\nPlease enter shell: "
read shell
echo "\nPlease enter home directory: "
read homedirectory
echo "\nPlease enter comment: "
read comment
echo "\nPlease enter username:"
read username
echo "\nPlease enter user id: "
read userid
echo "\nPlease enter group: "
read group
echo "\nPlease enter shell: "
read shell
echo "\nPlease enter home directory: "
read homedirectory
echo "\nPlease enter comment: "
read comment
useradd $username -u $userid -g $group -s $shell -d $homedirectory -c $comment"

You could even dump the info to a file so you can see all of the information that the user supplies:

echo "useradd $username -u $userid -g $group -s $shell -d $homedirectory -c $comment" >/tmp/useradd.test
Together We Stand!
Victor Fridyev
Honored Contributor

Re: useradd script help

Hi,

Don't forget about SAM. You can use an additional script which is called from SAM before and/or after user's creation. You can put into the script all additional features.
HTH
Entities are not to be multiplied beyond necessity - RTFM