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
How can you take dynamic user input in a Shell script? a) input b) read c) get d) receive
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
Which command allows a script to prompt the user for input and store it? a) echo b) print c) read d) ask
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
Which option in the read command is used to display a prompt to the user? a) -p b) -q c) -m d) -t
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
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
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
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
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
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
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
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
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
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
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
Which Shell command is used to check whether a string is empty? a) empty b) is_empty c) test -z "$string" d) check_empty
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
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
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
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
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
What command is typically used to display a menu in a Shell script? a) echo b) input c) menu d) show
Which statement is used to handle different menu options in Shell scripting? a) case b) if c) while d) switch
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
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
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
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
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
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
Qno
Answer
1
b) read
2
b) read variable
3
c) read
4
c) It will store the name in the variable name
5
a) -p
6
d) read -p "Name (default John): " name
7
b) read -p "Enter number: " number
8
b) It takes input and stores it in a variable
9
a) It will print the entered age
10
a) It reads a password silently (without showing input)
11
a) Using if statements and regular expressions
12
a) if [[ $input =~ ^[0-9]+$ ]]; then
13
c) Using a loop and validation check
14
a) It will print “Invalid” for negative or zero numbers
15
a) while loop with if statement
16
b) The input will be stored and used
17
c) test -z "$string"
18
a) if [ -z "$input" ]; then echo "Invalid"; fi
19
b) The script will ask the user to enter a valid option
20
a) test
21
b) To make the script user-friendly
22
a) Using echo statements and read for selection
23
a) echo
24
a) case
25
a) Using a while loop with read and exit
26
a) It will ask for user input after displaying the options
27
a) By using exit after the user selects the exit option
28
a) It allows for creating interactive menus
29
b) Using exit inside the menu options
30
a) It will show the selected option after the user input