Question

Write a script to automate the creation of new users and groups in Linux. ( I...

Write a script to automate the creation of new users and groups in Linux. ( I would like step by step instructions, including how to create the file) you can use user 1- user 5 as example.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Shell Script

#!/bin/bash

while [ x$username = "x" ]; do

read -p "Please enter the username you wish to create : " username

if id -u $username >/dev/null 2>&1; then

echo "User already exists"

username=""

fi

done

while [ x$group = "x" ]; do

read -p "Please enter the primary group. If group not exist, it will be created : " group

if id -g $group >/dev/null 2>&1; then

echo "Group exist"

else

groupadd $group

fi

done

read -p "Please enter bash [/bin/bash] : " bash

if [ x"$bash" = "x" ]; then

bash="/bin/bash"

fi

read -p "Please enter homedir [/home/$username] : " homedir

if [ x"$homedir" = "x" ]; then

homedir="/home/$username"

fi

read -p "Please confirm [y/n]" confirm

if [ "$confirm" = "y" ]; then

useradd -g $group -s $bash -d $homedir -m $username

fi

Sample Result

sudo ./linux_user.sh

Please enter the username you wish to create : user 1

Please enter the primary group. If group not exist, it will be created : group 1

Please enter bash [/bin/bash] :

Please enter homedir [/home/user 1] :

Please confirm [y/n] y

22:12:58 [test@Desktop] :~ id group 1

uid=1003(group 1) gid=1003(group 1) groups=1003(group 1)

Learn Above Script Line by Line

#Write to console ask to enter group and save input to group variable

read -p "Please enter the primary group. If group not exist, it will be created : " group

#check if group already exist

if id -g $group >/dev/null 2>&1; then

#just warn that group already exist

echo "Group exist"

else

#if group not exist – create one more

groupadd $group

fi

#end of while loop

done

#ask to enter preferred bash

read -p "Please enter bash [/bin/bash] : " bash

#check if no input

if [ x"$bash" = "x" ]; then

#if no input, use default bash

bash="/bin/bash"

fi

#ask to enter preferred homedir

read -p "Please enter homedir [/home/$username] : " homedir

#check if no input

if [ x"$homedir" = "x" ]; then

#if no input , use default homedir

homedir="/home/$username"

fi

#ask to confirm all inputs

read -p "Please confirm [y/n]" confirm

#if input y

if [ "$confirm" = "y" ]; then

#command to add user with all entered info

useradd -g $group -s $bash -d $homedir -m $username

fi

------------------------------------------------------------------------------------

hoping the description is enough........

Add a comment
Know the answer?
Add Answer to:
Write a script to automate the creation of new users and groups in Linux. ( I...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT