Bird
0
0

You want to write a script that asks the user for their age and then prints if they are an adult or not. Which script correctly uses input to make it interactive and works as expected?

hard🚀 Application Q15 of 15
Bash Scripting - User Input
You want to write a script that asks the user for their age and then prints if they are an adult or not. Which script correctly uses input to make it interactive and works as expected?
Option A:
read -p "Enter age: " age
if [ $age -ge 18 ]; then
  echo "Adult"
else
  echo "Not adult"
fi

Option B:
read age "Enter age: "
if [ $age -ge 18 ]; then
  echo "Adult"
else
  echo "Not adult"
fi

Option C:
read -p age "Enter age: "
if [ $age -ge 18 ]; then
  echo "Adult"
else
  echo "Not adult"
fi

Option D:
echo "Enter age: " | read age
if [ $age -ge 18 ]; then
  echo "Adult"
else
  echo "Not adult"
fi
AOption A
BOption B
COption C
DOption D
Step-by-Step Solution
Solution:
  1. Step 1: Check prompt and read syntax

    The correct usage is read -p "Enter age: " age to prompt and store input. Other options misuse syntax.
  2. Step 2: Verify conditional logic

    All options use the same if condition, which is correct. The difference is only in input reading.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Correct prompt and read syntax = A [OK]
Quick Trick: Use read -p before variable for input [OK]
Common Mistakes:
MISTAKES
  • Placing prompt after variable name
  • Using pipe incorrectly with read
  • Misplacing -p option

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes