MCQs on Interactive Scripts | Shell Scripting

Interactive scripts in Shell scripting allow developers to create dynamic, responsive scripts that take user input, validate it, and present menus for easy navigation. This set of MCQs will test your knowledge of these essential concepts.


Taking User Input Dynamically

  1. How can you take dynamic user input in a Shell script?
    a) input
    b) read
    c) get
    d) receive
  2. What is the correct way to store user input in a variable in Shell scripting?
    a) input variable
    b) read variable
    c) set variable
    d) receive variable
  3. Which command allows a script to prompt the user for input and store it?
    a) echo
    b) print
    c) read
    d) ask
  4. What is the output of this command?
    read -p "Enter your name: " name
    a) It will display the entered name
    b) It will prompt for the user’s name
    c) It will store the name in the variable name
    d) It will print “Enter your name: ” on the screen
  5. Which option in the read command is used to display a prompt to the user?
    a) -p
    b) -q
    c) -m
    d) -t
  6. How do you capture user input with a default value using read?
    a) read name=John
    b) read -d "John" name
    c) read name (if input is empty, default is used)
    d) read -p "Name (default John): " name
  7. Which of the following is the correct way to take numeric input from a user in Shell?
    a) read -n number
    b) read -p "Enter number: " number
    c) get number
    d) input number
  8. What is the purpose of the read command in Shell scripting?
    a) It displays a message
    b) It takes input and stores it in a variable
    c) It runs a command
    d) It pauses the script
  9. What does the following command do?
    read -p "Enter age: " age; echo "Your age is $age"
    a) It stores the age in the age variable and prints it
    b) It prints “Enter age: “
    c) It executes a loop
    d) It doesn’t work properly
  10. What will the following command do?
    read -s password; echo "Your password is $password"
    a) It reads a password silently (without showing input)
    b) It displays the entered password
    c) It prompts for a password twice
    d) It doesn’t work as expected

Validating Input

  1. How can you validate that a user has entered a numeric value in Shell scripting?
    a) Using if statements and regular expressions
    b) Using the validate command
    c) Using the is_numeric function
    d) By checking the length of the input
  2. Which of the following is the correct syntax to validate if user input is a number?
    a) if [[ $input =~ ^[0-9]+$ ]]; then
    b) if [ $input -eq 0 ]; then
    c) if $input -isNumeric; then
    d) if [ $input == "number" ]; then
  3. How can you prompt the user again for input if the first input is invalid?
    a) Using continue
    b) Using exit
    c) Using a loop and validation check
    d) Using break
  4. What will the following code snippet do?
    read -p "Enter a positive number: " num; if [[ $num -le 0 ]]; then echo "Invalid"; fi
    a) It will print “Invalid” for negative or zero numbers
    b) It will only accept numbers greater than 0
    c) It will print “Invalid” if the user enters a non-numeric value
    d) It will ask for input again
  5. Which of the following is used to ensure that the user enters a non-empty value?
    a) while loop with if statement
    b) continue statement
    c) exit statement
    d) test statement
  6. What happens if the read command is used without validation?
    a) The script may fail
    b) The input will be stored and used
    c) The script will prompt the user for valid input
    d) The input will be ignored
  7. Which Shell command is used to check whether a string is empty?
    a) empty
    b) is_empty
    c) test -z "$string"
    d) check_empty
  8. How can you validate that a user input is not empty?
    a) if [ -z "$input" ]; then echo "Invalid"; fi
    b) if [ -n "$input" ]; then echo "Valid"; fi
    c) if "$input" != ""; then
    d) if input != null; then
  9. What will happen if a user enters an invalid option in a menu without validation?
    a) The script will crash
    b) The script will ask the user to enter a valid option
    c) The script will exit immediately
    d) The script will loop infinitely
  10. Which of the following commands can be used for basic input validation?
    a) test
    b) validate
    c) check_input
    d) input_validate

Creating Interactive Menus

  1. What is the purpose of creating interactive menus in Shell scripting?
    a) To automate repetitive tasks
    b) To make the script user-friendly
    c) To debug the script
    d) To create system backups
  2. How do you create a simple menu with options in Shell scripting?
    a) Using echo statements and read for selection
    b) Using for loops only
    c) Using case statements alone
    d) Using goto statements
  3. What command is typically used to display a menu in a Shell script?
    a) echo
    b) input
    c) menu
    d) show
  4. Which statement is used to handle different menu options in Shell scripting?
    a) case
    b) if
    c) while
    d) switch
  5. How can you loop a menu until the user selects the option to exit?
    a) Using a while loop with read and exit
    b) Using continue with break
    c) Using a for loop to display options
    d) Using goto
  6. What will the following menu script display?
    echo "1. Option 1"; echo "2. Option 2"; read choice;
    a) It will ask for user input after displaying the options
    b) It will only display option 1
    c) It will show the selected option
    d) It will display an error
  7. How can you exit an interactive menu in Shell scripting?
    a) By using exit after the user selects the exit option
    b) By using continue
    c) By using break
    d) By using end
  8. What is the purpose of the select command in Shell scripting?
    a) It allows for creating interactive menus
    b) It selects files for processing
    c) It selects a variable to store data
    d) It selects a system for installation
  9. How would you implement an exit option in a Shell menu?
    a) Using a while loop with break
    b) Using exit inside the menu options
    c) Using continue in the menu
    d) Using end command
  10. What is the output of the following code?
    echo "1) Option 1"; echo "2) Option 2"; select option in Option1 Option2; do echo "You selected $option"; break; done
    a) It will show the selected option after the user input
    b) It will show an error message
    c) It will ask for input again after selection
    d) It will exit the script immediately

Answer Key

QnoAnswer
1b) read
2b) read variable
3c) read
4c) It will store the name in the variable name
5a) -p
6d) read -p "Name (default John): " name
7b) read -p "Enter number: " number
8b) It takes input and stores it in a variable
9a) It will print the entered age
10a) It reads a password silently (without showing input)
11a) Using if statements and regular expressions
12a) if [[ $input =~ ^[0-9]+$ ]]; then
13c) Using a loop and validation check
14a) It will print “Invalid” for negative or zero numbers
15a) while loop with if statement
16b) The input will be stored and used
17c) test -z "$string"
18a) if [ -z "$input" ]; then echo "Invalid"; fi
19b) The script will ask the user to enter a valid option
20a) test
21b) To make the script user-friendly
22a) Using echo statements and read for selection
23a) echo
24a) case
25a) Using a while loop with read and exit
26a) It will ask for user input after displaying the options
27a) By using exit after the user selects the exit option
28a) It allows for creating interactive menus
29b) Using exit inside the menu options
30a) It will show the selected option after the user input

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top