Introduction
In this challenge, you will create a bash script named userctr.sh to automate the batch addition and deletion of users on a server. You will write a script that accepts command-line arguments to specify the operation (add or del), a teacher's username, a prefix for student usernames, and the number of students to manage. This task will enhance your skills in shell scripting, user management, and automation, which are crucial for system administration.
Users and Groups Creation and Deletion Batch
A systems administrator needs a way to efficiently add a teacher and multiple student users to classroom servers. Manually creating each account is tedious and prone to errors. Your task is to create a bash script, userctr.sh, to automate this process. The script will take the operation type, teacher's name, student name prefix, and the number of students as parameters.
Tasks
- Create a Bash script named
userctr.shin the/home/labex/projectdirectory. - The script must implement two main operations:
addanddel, determined by the first command-line argument. - For the
addoperation, create a teacher user and a specified number of student users. - For the
deloperation, delete the specified teacher and student users. - The script must validate all input parameters according to the requirements below.
Requirements
Execution Format: The script must be executable with four arguments:
sh userctr.sh <operation> <teacher_name> <student_prefix> <student_count>Parameter Validation:
- The script must receive exactly four arguments. If not, it should print
parameter errorand exit. - The
student_count(fourth argument) must be an integer between 1 and 10, inclusive. Otherwise, printparameter errorand exit. - The
student_prefix(third argument) must consist of only lowercase letters. Otherwise, printparameter errorand exit.
- The script must receive exactly four arguments. If not, it should print
addOperation Details:- If a user to be added already exists, do not attempt to create them. Instead, print their username followed by
******(e.g.,stu3:******). - For new users, generate a random 6-digit numeric password.
- Print the username and its password for each newly created user, separated by a colon (e.g.,
teacher:901231). - All created users must use
/bin/zshas their default shell. - The teacher user must be added to the
sudogroup to grant administrative privileges.
- If a user to be added already exists, do not attempt to create them. Instead, print their username followed by
delOperation Details:- The script should delete the specified teacher and student users, including their home directories.
- The
deloperation should not produce any output. - If a user to be deleted does not exist, the script should not report an error and should proceed with deleting the other existing users.
Example
The following examples demonstrate the expected behavior of your script.
Adding Users: This command adds one teacher (
teacher) and six students (stu1tostu6).sh userctr.sh add teacher stu 6Example Output:
teacher:901231 stu1:271828 stu2:928172 stu3:****** stu4:384712 stu5:098273 stu6:921098In this output, the asterisks for
stu3indicate that the user already existed, so a new password was not generated.Deleting Users: This command deletes the same set of users. It should produce no output upon successful completion.
sh userctr.sh del teacher stu 6
Hints
- Use
sudobefore commands that require root privileges, likeuseraddanduserdel. - Check the number of arguments with
$#. - Access arguments using
$1,$2, etc. - To check if a user exists, you can use
id -u <username>. The command will have a non-zero exit code if the user does not exist. - Use
useradd -m -s /bin/zsh <username>to create a user with a home directory and set the Zsh shell. - Use
usermod -aG sudo <username>to add a user to thesudogroup. - Use
userdel -r <username>to delete a user and their home directory. - To generate a random 6-digit number, you can use
shuf -i 100000-999999 -n 1. - A
forloop is useful for iterating through the number of students:for ((i=1; i<=$student_count; i++)). - Use
if [[ "$string" =~ "regex" ]]for pattern matching to validate parameters.
Summary
Congratulations! You have successfully created a bash script to automate user management. This script can add and delete users in batches, validate input parameters, and handle existing users, which are essential skills for any system administrator. You have practiced using conditional statements, loops, command-line arguments, and external commands like useradd and userdel within a script.



