Bird
0
0

You want to write a script that lists all users who have a valid login shell (not /usr/sbin/nologin or /bin/false) by reading /etc/passwd. Which command snippet correctly filters these users?

hard📝 Application Q15 of 15
Linux CLI - Users and Groups
You want to write a script that lists all users who have a valid login shell (not /usr/sbin/nologin or /bin/false) by reading /etc/passwd. Which command snippet correctly filters these users?
Aawk -F':' '$7 != "/usr/sbin/nologin" && $7 != "/bin/false" {print $1}' /etc/passwd
Bgrep -v '/usr/sbin/nologin' /etc/shadow | cut -d':' -f1
Ccat /etc/passwd | grep '/bin/bash' | awk '{print $1}'
Dsed '/nologin/d' /etc/passwd | cut -d':' -f7
Step-by-Step Solution
Solution:
  1. Step 1: Identify shell field in /etc/passwd

    The 7th field in /etc/passwd is the user's login shell. We want to exclude /usr/sbin/nologin and /bin/false.
  2. Step 2: Analyze each option

    awk -F':' '$7 != "/usr/sbin/nologin" && $7 != "/bin/false" {print $1}' /etc/passwd uses awk with field separator ':' and filters out unwanted shells, printing usernames (field 1). grep -v '/usr/sbin/nologin' /etc/shadow | cut -d':' -f1 wrongly reads /etc/shadow and uses grep -v incorrectly. cat /etc/passwd | grep '/bin/bash' | awk '{print $1}' filters only /bin/bash but misses other valid shells. sed '/nologin/d' /etc/passwd | cut -d':' -f7 deletes lines with 'nologin' but then prints shell field, not usernames.
  3. Final Answer:

    awk -F':' '$7 != "/usr/sbin/nologin" && $7 != "/bin/false" {print $1}' /etc/passwd -> Option A
  4. Quick Check:

    Use awk on /etc/passwd to filter shell field and print usernames [OK]
Quick Trick: Use awk with FS=':' and check field 7 for valid shells [OK]
Common Mistakes:
  • Using /etc/shadow instead of /etc/passwd for shell info
  • Printing wrong fields (shell instead of username)
  • Using grep without proper field filtering

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Linux CLI Quizzes