User account management script in Bash Scripting - Time & Space Complexity
When running a user account management script, it is important to understand how the time it takes grows as the number of users increases.
We want to know how the script's work changes when managing more user accounts.
Analyze the time complexity of the following code snippet.
#!/bin/bash
users=("alice" "bob" "carol" "dave")
for user in "${users[@]}"; do
id "$user" >/dev/null 2>&1
if [ $? -ne 0 ]; then
useradd "$user"
fi
passwd -l "$user"
done
This script checks if each user exists, adds them if not, and then locks their password.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that goes through each user in the list.
- How many times: Once for each user in the users array.
As the number of users grows, the script runs the same steps for each user one after another.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and updates |
| 100 | About 100 checks and updates |
| 1000 | About 1000 checks and updates |
Pattern observation: The work grows directly in proportion to the number of users.
Time Complexity: O(n)
This means the time taken grows linearly with the number of users to manage.
[X] Wrong: "The script runs in constant time no matter how many users there are."
[OK] Correct: Because the script does work for each user, more users mean more work and more time.
Understanding how scripts scale with input size shows you can write efficient automation that handles growing tasks smoothly.
"What if the script also checked each user's group memberships inside the loop? How would the time complexity change?"
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
