Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand the commands used

    The commands useradd and userdel are specifically for managing user accounts on Linux systems.
  2. 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.
  3. Final Answer:

    To automate adding, deleting, and listing users on the system -> Option B
  4. 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

  1. Step 1: Recall the standard command for adding users

    The standard Linux command to add a user is useradd.
  2. Step 2: Match the command with the username

    Using useradd alice correctly adds a user named alice.
  3. Final Answer:

    useradd alice -> Option D
  4. 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

  1. 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.
  2. 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".
  3. Final Answer:

    User does not exist -> Option A
  4. 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

  1. Step 1: Check command permissions

    The userdel command requires root privileges. Running it without sudo or as root will fail.
  2. Step 2: Verify script syntax and commands

    The if statement syntax and command are correct, but permission issues cause failure.
  3. Final Answer:

    Missing sudo or root privileges to run userdel -> Option A
  4. 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

  1. Step 1: Understand reading lines safely

    Using while read user reads each line safely, including spaces, and allows skipping empty lines.
  2. Step 2: Check skipping empty lines and adding users

    The condition [ -z "$user" ] && continue skips empty lines. Then useradd "$user" adds the user correctly.
  3. Final Answer:

    while read user; do [ -z "$user" ] && continue useradd "$user" done < users.txt -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard