Bird
0
0

How can you combine default value substitution with a conditional check to print "Valid" only if input is non-empty, else print "Default used"?

hard🚀 Application Q9 of 15
Bash Scripting - User Input
How can you combine default value substitution with a conditional check to print "Valid" only if input is non-empty, else print "Default used"?
Aif [ -z "$input" ]; then echo "Valid"; else echo "Default used"; fi
Becho "${input:+Valid}" || echo "Default used"
Cecho "${input:-Default used}" && echo "Valid"
Dif [ -n "$input" ]; then echo "Valid"; else echo "Default used"; fi
Step-by-Step Solution
Solution:
  1. Step 1: Understand conditional checks for non-empty input

    [ -n "$input" ] tests if input is non-empty.
  2. Step 2: Use if-else to print correct message

    If non-empty, print "Valid"; else print "Default used".
  3. Final Answer:

    if [ -n "$input" ]; then echo "Valid"; else echo "Default used"; fi -> Option D
  4. Quick Check:

    Check non-empty with -n for conditional output = A [OK]
Quick Trick: Use if [ -n "$var" ] to check non-empty before printing [OK]
Common Mistakes:
MISTAKES
  • Confusing -n and -z tests
  • Using parameter expansion incorrectly for conditional output
  • Using || or && incorrectly for conditional prints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes