This script helps you add, delete, or list user accounts on a Linux system easily. It saves time and avoids mistakes when managing users.
User account management script in Bash Scripting
#!/bin/bash case "$1" in add) sudo useradd "$2" echo "User $2 added." ;; delete) sudo userdel "$2" echo "User $2 deleted." ;; list) cut -d: -f1 /etc/passwd ;; *) echo "Usage: $0 {add|delete|list} [username]" ;; esac
The script uses case to handle different commands.
sudo is needed to run user management commands with proper permissions.
./user_script.sh add alice
./user_script.sh delete bob
./user_script.sh listThis script checks the first argument to decide what to do: add, delete, or list users. It also checks if the username is given when adding or deleting.
#!/bin/bash case "$1" in add) if [ -z "$2" ]; then echo "Please provide a username to add." exit 1 fi sudo useradd "$2" && echo "User $2 added." ;; delete) if [ -z "$2" ]; then echo "Please provide a username to delete." exit 1 fi sudo userdel "$2" && echo "User $2 deleted." ;; list) cut -d: -f1 /etc/passwd ;; *) echo "Usage: $0 {add|delete|list} [username]" exit 1 ;; esac
Always run this script with proper permissions (usually with sudo) to manage users.
Be careful when deleting users to avoid losing important data.
You can extend this script to add passwords or other user settings.
This script automates adding, deleting, and listing users.
It uses simple commands and checks for required inputs.
Running with correct permissions is important for success.
Practice
useradd and userdel?Solution
Step 1: Understand the commands used
The commandsuseraddanduserdelare specifically for managing user accounts on Linux systems.Step 2: Identify the script's goal
Since the script uses these commands, its main purpose is to automate user account management tasks like adding, deleting, and listing users.Final Answer:
To automate adding, deleting, and listing users on the system -> Option BQuick Check:
User management commands = automate user tasks [OK]
- Confusing user management with system updates
- Thinking it changes network settings
- Assuming it modifies system hostname
alice in a bash script?Solution
Step 1: Recall the standard command for adding users
The standard Linux command to add a user isuseradd.Step 2: Match the command with the username
Usinguseradd alicecorrectly adds a user named alice.Final Answer:
useradd alice -> Option DQuick Check:
Standard add user command = useradd [OK]
- Using adduser which may not be available on all systems
- Typing createuser or newuser which are invalid commands
- Confusing commands with other system utilities
if id "bob" &>/dev/null; then echo "User exists" else echo "User does not exist" fi
Solution
Step 1: Understand the
The commandidcommand usageid "bob"checks if user bob exists. If bob does not exist, it returns an error.Step 2: Analyze the if condition and output
The script redirects errors to /dev/null, so if bob does not exist, the else branch runs, printing "User does not exist".Final Answer:
User does not exist -> Option AQuick Check:
id command fails = else branch runs [OK]
- Assuming output shows error messages
- Thinking permission issues cause output
- Confusing command syntax with errors
charlie:userdel charlie if [ $? -eq 0 ]; then echo "User deleted" else echo "Failed to delete user" fi
Solution
Step 1: Check command permissions
Theuserdelcommand requires root privileges. Running it without sudo or as root will fail.Step 2: Verify script syntax and commands
The if statement syntax and command are correct, but permission issues cause failure.Final Answer:
Missing sudo or root privileges to run userdel -> Option AQuick Check:
userdel needs root = permission error if missing [OK]
- Thinking userdel syntax is wrong
- Using wrong command name like userremove
- Ignoring permission requirements
users.txt. Which script snippet correctly reads each username and adds the user, skipping empty lines?Solution
Step 1: Understand reading lines safely
Usingwhile read userreads each line safely, including spaces, and allows skipping empty lines.Step 2: Check skipping empty lines and adding users
The condition[ -z "$user" ] && continueskips empty lines. Thenuseradd "$user"adds the user correctly.Final Answer:
while read user; do [ -z "$user" ] && continue useradd "$user" done < users.txt -> Option CQuick Check:
Safe line read + skip empty + add user = while read user; do [ -z "$user" ] && continue useradd "$user" done < users.txt [OK]
- Using for loop with word splitting causing errors
- Incorrect pipe usage with cat and while
- Not skipping empty lines causing useradd errors
Ready to prove it?
Pass the test and this pattern moves unknown
