Bird
0
0

How to write a bash condition that prints "Valid" only if name is not empty and not equal to "admin"?

hard🚀 Application Q9 of 15
Bash Scripting - Conditionals
How to write a bash condition that prints "Valid" only if name is not empty and not equal to "admin"?
Aif [[ -z "$name" || "$name" = "admin" ]]; then echo "Valid"; fi
Bif [ -z "$name" -o "$name" = "admin" ]; then echo "Valid"; fi
Cif [[ -n "$name" && "$name" != "admin" ]]; then echo "Valid"; fi
Dif [ -n "$name" -a "$name" != "admin" ]; then echo "Valid"; fi
Step-by-Step Solution
Solution:
  1. Step 1: Understand the condition

    We want to print "Valid" if name is not empty AND not "admin".
  2. Step 2: Analyze options

    if [[ -n "$name" && "$name" != "admin" ]]; then echo "Valid"; fi uses [[ ]] with -n and && and quotes variables correctly. if [ -z "$name" -o "$name" = "admin" ]; then echo "Valid"; fi and C check opposite conditions. if [ -n "$name" -a "$name" != "admin" ]; then echo "Valid"; fi uses [ ] with -a which is less recommended.
  3. Final Answer:

    if [[ -n "$name" && "$name" != "admin" ]]; then echo "Valid"; fi -> Option C
  4. Quick Check:

    Use [[ ]] with -n and && for AND conditions [OK]
Quick Trick: Use [[ ]] with -n and && for AND string checks [OK]
Common Mistakes:
MISTAKES
  • Using -z instead of -n
  • Mixing AND/OR incorrectly
  • Not quoting variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes