MCQs on Advanced Scripting Techniques | Shell Scripting

Mastering Advanced Scripting Techniques: Dynamic Commands, Signal Handling, and Daemons
Explore advanced scripting techniques like using eval for dynamic commands, implementing signal handling with trap, and writing daemons to manage background tasks efficiently in Linux systems.


MCQs on Advanced Scripting Techniques

1. Using eval for Dynamic Commands

  1. What is the primary purpose of the eval command in shell scripting?
    a) To execute a string as a shell command
    b) To evaluate arithmetic expressions
    c) To run multiple commands sequentially
    d) To execute background tasks
  2. Which of the following best describes a scenario where eval might be used?
    a) Running a command with arguments passed as variables
    b) Printing values of variables
    c) Redirecting the output of a command
    d) Setting environment variables
  3. Which of the following is a key risk when using the eval command?
    a) It can lead to errors in variable interpretation
    b) It can execute malicious code if input is not controlled
    c) It can slow down the system
    d) It can only be used in interactive shells
  4. What will the following command do? eval "echo \$USER"
    a) It will print the value of the $USER environment variable
    b) It will attempt to execute the command \$USER
    c) It will print the literal text \$USER
    d) It will raise an error due to incorrect syntax
  5. What is one correct usage of eval to execute a string command in shell scripting?
    a) eval 'echo Hello'
    b) eval echo "Hello"
    c) eval "echo 'Hello'"
    d) eval echo Hello
  6. When would using eval be particularly useful in shell scripting?
    a) When the script needs to perform string manipulation
    b) When executing commands that are dynamically constructed
    c) When managing large sets of static commands
    d) When checking the system status
  7. What does the eval command do to special characters in a string?
    a) It escapes all special characters
    b) It evaluates the string as a valid shell command
    c) It removes special characters
    d) It prints the special characters as part of the output
  8. Which of the following will not work with eval?
    a) eval "echo \$USER"
    b) eval "echo $USER"
    c) eval "echo \\$USER"
    d) eval "echo $USER | grep home"
  9. How does eval handle nested commands?
    a) It evaluates them from the inside out
    b) It executes the outer command and ignores the inner ones
    c) It only evaluates the first command in the string
    d) It throws an error due to nested commands
  10. What would the command eval "ls $directory" do?
    a) List the contents of the variable directory
    b) Attempt to execute the value of $directory as a command
    c) List all directories in the current path
    d) Raise an error

2. Implementing Traps for Signal Handling

  1. What is the purpose of the trap command in shell scripting?
    a) To monitor running processes
    b) To handle signals and perform cleanup tasks
    c) To log system activities
    d) To handle command-line arguments
  2. Which of the following is the correct syntax for using trap in a shell script?
    a) trap SIGNAL COMMAND
    b) trap COMMAND SIGNAL
    c) trap - SIGNAL COMMAND
    d) trap SIGNAL ON_COMMAND
  3. What is the role of a signal handler in trap?
    a) It terminates the process
    b) It executes a specific command when a signal is received
    c) It pauses the script execution
    d) It ignores incoming signals
  4. Which signal is used to stop a process in Linux?
    a) SIGINT
    b) SIGSTOP
    c) SIGTERM
    d) SIGKILL
  5. What does the following trap command do? trap 'echo "Terminating script"' SIGTERM
    a) It stops the script when the SIGTERM signal is received
    b) It prints “Terminating script” when SIGTERM is received
    c) It runs the command echo "Terminating script" every time SIGTERM occurs
    d) It sends SIGTERM to the script
  6. How would you ignore a specific signal in a shell script using trap?
    a) trap '' SIGNAL
    b) trap - SIGNAL
    c) trap SIGNAL ignore
    d) trap -
  7. How would you handle multiple signals with a single trap command?
    a) trap 'command' SIGNAL1 SIGNAL2
    b) trap SIGNAL1 SIGNAL2 'command'
    c) trap SIGNAL1,SIGNAL2 'command'
    d) trap 'command' SIGNAL1 && SIGNAL2
  8. What is the default action when a signal is received if no trap is set?
    a) The script terminates immediately
    b) The signal is ignored
    c) The script waits for the signal
    d) The signal is handled by the default handler
  9. Which of the following signals can be trapped in a script?
    a) SIGQUIT
    b) SIGKILL
    c) SIGSTOP
    d) SIGABRT
  10. What does the command trap 'echo "Exit Signal Received"' EXIT do?
    a) Prints a message when the script exits
    b) Sends an EXIT signal to the script
    c) Immediately terminates the script
    d) Executes the EXIT signal in the background

3. Writing Daemons

  1. What is a daemon in Linux?
    a) A background process that runs indefinitely
    b) A foreground process that requires user interaction
    c) A process that monitors network activity
    d) A process that terminates on exit
  2. Which of the following commands is commonly used to start a daemon process?
    a) service start
    b) nohup
    c) start_daemon
    d) daemon_process
  3. In the context of a daemon, what does nohup do?
    a) It runs the process in the background and prevents it from being terminated when the shell exits
    b) It runs the process with higher priority
    c) It runs the process in the background and ignores signals
    d) It logs all outputs of the process to a file
  4. How do you handle output in a daemon?
    a) Redirect it to a log file using >
    b) Let the daemon handle the output automatically
    c) Use the exit command to terminate the daemon’s output
    d) Output will be sent to the terminal by default
  5. Which signal is commonly used to stop a daemon process?
    a) SIGSTOP
    b) SIGKILL
    c) SIGTERM
    d) SIGABRT
  6. How do you daemonize a process in a shell script?
    a) By redirecting output to /dev/null and using &
    b) By running the process in the foreground
    c) By setting the process to run as root
    d) By using the start command
  7. Which of the following is true about writing daemons?
    a) Daemons should handle signals like SIGTERM and SIGHUP
    b) Daemons can only be written in C
    c) Daemons cannot be controlled from the terminal
    d) Daemons must run in the same session as the user
  8. How do daemons usually handle logging?
    a) They write logs to /var/log/
    b) They write logs to standard output
    c) They don’t log at all
    d) They send logs to the system admin’s email
  9. What is the best practice for writing a daemon that handles errors?
    a) Use signal handlers and redirect logs to a file
    b) Ignore errors to keep the process running
    c) Stop the daemon when an error occurs
    d) Restart the daemon every time an error happens
  10. What is the main reason to use a daemon in system administration?
    a) To run a process in the background without user intervention
    b) To speed up system processes
    c) To handle administrative privileges
    d) To enable user interaction with the system

Answers

QnoAnswer
1a) To execute a string as a shell command
2a) Running a command with arguments passed as variables
3b) It can execute malicious code if input is not controlled
4a) It will print the value of the $USER environment variable
5c) eval "echo 'Hello'"
6b) When executing commands that are dynamically constructed
7b) It evaluates the string as a valid shell command
8d) `eval “echo $USER
9a) It evaluates them from the inside out
10a) List the contents of the variable directory
11b) To handle signals and perform cleanup tasks
12a) trap SIGNAL COMMAND
13b) It executes a specific command when a signal is received
14b) SIGSTOP
15b) It prints “Terminating script” when SIGTERM is received
16a) trap '' SIGNAL
17a) trap 'command' SIGNAL1 SIGNAL2
18a) The script terminates immediately
19a) SIGQUIT
20a) Prints a message when the script exits
21a) A background process that runs indefinitely
22b) nohup
23a) It runs the process in the background and prevents it from being terminated when the shell exits
24a) Redirect it to a log file using >
25c) SIGTERM
26a) By redirecting output to /dev/null and using &
27a) Daemons should handle signals like SIGTERM and SIGHUP
28a) They write logs to /var/log/
29a) Use signal handlers and redirect logs to a file
30a) To run a process in the background without user intervention

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