0
0
Bash Scriptingscripting~20 mins

User account management script in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
User Account Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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
AUser alice created
BError: useradd command not found
CUser alice already exists
DPassword set for alice
Attempts:
2 left
💡 Hint
The script checks if the user exists before creating.
📝 Syntax
intermediate
2: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
AAdd 'then' after the if condition: if [ $? -eq 0 ]; then
BReplace 'fi' with 'end'
CRemove the else block entirely
DAdd a semicolon after echo statements
Attempts:
2 left
💡 Hint
Bash if statements require 'then' after the condition.
🔧 Debug
advanced
2: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
AThe script lacks quotes around $user causing word splitting
BThe script does not handle empty lines or spaces in usernames
CThe input redirection syntax is incorrect
DThe read command trims trailing spaces causing empty usernames
Attempts:
2 left
💡 Hint
Variables with spaces need quotes in bash commands.
🚀 Application
advanced
2: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
AThe total number of users in /etc/passwd
BZero, because the script has a syntax error
CThe number of users without home directories
DThe number of users with home directories under /home
Attempts:
2 left
💡 Hint
The script filters users by home directory path prefix.
🧠 Conceptual
expert
2: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?
ATo create a new user account with default settings
BTo modify existing user account properties like username, home directory, or shell
CTo delete a user account and remove its files
DTo list all current user accounts on the system
Attempts:
2 left
💡 Hint
Think about changing user details without deleting or creating.