📖 Scenario: You are a system administrator who needs to manage user accounts on a Linux server. You want to automate the process of checking user accounts and their statuses.
🎯 Goal: Create a Bash script that stores a list of user accounts with their statuses, sets a status filter, filters the users based on that status, and then prints the filtered list.
📋 What You'll Learn
Create an associative array called users with usernames as keys and their statuses as values.
Create a variable called filter_status to hold the status to filter users by.
Use a for loop to iterate over the users array and select users whose status matches filter_status.
Print the filtered usernames and their statuses.
💡 Why This Matters
🌍 Real World
System administrators often need to manage user accounts and their statuses on servers. Automating this with scripts saves time and reduces errors.
💼 Career
Knowing how to write scripts for user account management is valuable for roles in IT support, system administration, and DevOps.
Progress0 / 4 steps
1
Create the user accounts data
Create an associative array called users with these exact entries: alice with status active, bob with status inactive, carol with status active, and dave with status inactive.
Bash Scripting
Hint
Use declare -A users to create an associative array in Bash.
2
Set the status filter
Create a variable called filter_status and set it to the string active.
Bash Scripting
Hint
Assign the string active to the variable filter_status.
3
Filter users by status
Use a for loop with the variable user to iterate over the keys of the users array. Inside the loop, use an if statement to check if the user's status matches filter_status. If it matches, add the user and their status to a new associative array called filtered_users.
Bash Scripting
Hint
Use for user in "${!users[@]}" to loop over keys of an associative array.
4
Print the filtered users
Use a for loop with the variable user to iterate over the keys of the filtered_users array. Inside the loop, print the username and their status in the format: user: status.
Bash Scripting
Hint
Use echo "$user: ${filtered_users[$user]}" inside the loop to print each filtered user.
Practice
(1/5)
1. What is the main purpose of a bash script that manages user accounts with commands like useradd and userdel?
easy
A. To change the system hostname
B. To automate adding, deleting, and listing users on the system
C. To update software packages automatically
D. To monitor network traffic
Solution
Step 1: Understand the commands used
The commands useradd and userdel are 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 B
Quick Check:
User management commands = automate user tasks [OK]
Hint: Look for user management commands like useradd/userdel [OK]
Common Mistakes:
Confusing user management with system updates
Thinking it changes network settings
Assuming it modifies system hostname
2. Which of the following is the correct syntax to add a new user named alice in a bash script?
easy
A. createuser alice
B. adduser alice
C. newuser alice
D. useradd alice
Solution
Step 1: Recall the standard command for adding users
The standard Linux command to add a user is useradd.
Step 2: Match the command with the username
Using useradd alice correctly adds a user named alice.
Final Answer:
useradd alice -> Option D
Quick Check:
Standard add user command = useradd [OK]
Hint: Remember: useradd is the standard command to add users [OK]
Common Mistakes:
Using adduser which may not be available on all systems
Typing createuser or newuser which are invalid commands
Confusing commands with other system utilities
3. What will be the output of this bash snippet if the script is run with root privileges?
if id "bob" &>/dev/null; then
echo "User exists"
else
echo "User does not exist"
fi
medium
A. User does not exist
B. User exists
C. Syntax error
D. Permission denied
Solution
Step 1: Understand the id command usage
The command id "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 A
Quick Check:
id command fails = else branch runs [OK]
Hint: id command success means user exists; else means no user [OK]
Common Mistakes:
Assuming output shows error messages
Thinking permission issues cause output
Confusing command syntax with errors
4. Identify the error in this script snippet that tries to delete a user named charlie:
userdel charlie
if [ $? -eq 0 ]; then
echo "User deleted"
else
echo "Failed to delete user"
fi
medium
A. Missing sudo or root privileges to run userdel
B. Incorrect syntax for if statement
C. userdel command should be userremove
D. No error, script works fine
Solution
Step 1: Check command permissions
The userdel command 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 A
Quick Check:
userdel needs root = permission error if missing [OK]
Hint: Remember: userdel needs root or sudo to succeed [OK]
Common Mistakes:
Thinking userdel syntax is wrong
Using wrong command name like userremove
Ignoring permission requirements
5. You want to write a bash script that adds multiple users from a list in a file users.txt. Which script snippet correctly reads each username and adds the user, skipping empty lines?
hard
A. read users.txt | while read user; do
useradd $user
done
B. for user in $(cat users.txt); do
useradd $user
done
C. while read user; do
[ -z "$user" ] && continue
useradd "$user"
done < users.txt
D. cat users.txt | while useradd; do
echo $user
done
Solution
Step 1: Understand reading lines safely
Using while read user reads each line safely, including spaces, and allows skipping empty lines.
Step 2: Check skipping empty lines and adding users
The condition [ -z "$user" ] && continue skips empty lines. Then useradd "$user" adds the user correctly.
Final Answer:
while read user; do
[ -z "$user" ] && continue
useradd "$user"
done < users.txt -> Option C
Quick Check:
Safe line read + skip empty + add user = while read user; do
[ -z "$user" ] && continue
useradd "$user"
done < users.txt [OK]
Hint: Use while read and check empty lines to avoid errors [OK]
Common Mistakes:
Using for loop with word splitting causing errors
Incorrect pipe usage with cat and while
Not skipping empty lines causing useradd errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.