Challenge - 5 Problems
User Account Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this user creation script snippet?
Consider this bash script snippet that attempts to add a user and set a password. What will be the output if the user 'alice' already exists on the system?
Bash Scripting
if id "alice" &>/dev/null; then echo "User alice already exists" else sudo useradd alice echo "alice:password123" | sudo chpasswd echo "User alice created" fi
Attempts:
2 left
💡 Hint
The script checks if the user exists before creating.
✗ Incorrect
The script uses 'id alice' to check if the user exists. If yes, it prints 'User alice already exists' and skips creation.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this user deletion script
Which option correctly fixes the syntax error in this bash script that deletes a user and their home directory?
Original snippet:
sudo userdel -r $username
if [ $? -eq 0 ]
echo "User deleted"
else
echo "Failed to delete user"
fi
Bash Scripting
sudo userdel -r $username if [ $? -eq 0 ]; then echo "User deleted" else echo "Failed to delete user" fi
Attempts:
2 left
💡 Hint
Bash if statements require 'then' after the condition.
✗ Incorrect
The 'if' statement must have 'then' after the condition to be valid syntax.
🔧 Debug
advanced2:00remaining
Why does this script fail to add multiple users from a file?
This script reads usernames from a file and tries to add them. Why does it fail to add any users?
Script:
while read user; do
sudo useradd $user
echo "Added $user"
done < users.txt
Bash Scripting
while read user; do sudo useradd "$user" echo "Added $user" done < users.txt
Attempts:
2 left
💡 Hint
Variables with spaces need quotes in bash commands.
✗ Incorrect
Without quotes, usernames with spaces split into multiple arguments causing useradd to fail.
🚀 Application
advanced2:00remaining
What is the value of the variable 'count' after running this script?
This script counts how many users in the system have a home directory under /home. What is the value of 'count' after execution?
Script:
count=0
for user in $(cut -d: -f1 /etc/passwd); do
home_dir=$(getent passwd "$user" | cut -d: -f6)
if [[ $home_dir == /home/* ]]; then
((count++))
fi
done
echo $count
Bash Scripting
count=0 for user in $(cut -d: -f1 /etc/passwd); do home_dir=$(getent passwd "$user" | cut -d: -f6) if [[ $home_dir == /home/* ]]; then ((count++)) fi done echo $count
Attempts:
2 left
💡 Hint
The script filters users by home directory path prefix.
✗ Incorrect
The script increments count only for users whose home directory starts with /home.
🧠 Conceptual
expert2:00remaining
Which option best describes the purpose of the 'usermod' command in user management scripts?
In bash scripts managing users, what is the primary purpose of the 'usermod' command?
Attempts:
2 left
💡 Hint
Think about changing user details without deleting or creating.
✗ Incorrect
The 'usermod' command changes properties of existing users, such as username or shell.