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