MCQs on Building and Packaging Shell Scripts | Shell

Shell scripting is an essential tool for automating tasks. Writing portable scripts, packaging them for distribution, and ensuring proper documentation and best practices are key concepts. This set of MCQs will test your knowledge on these vital areas in Shell scripting.


Writing Portable Scripts

  1. What makes a Shell script portable?
    a) Using specific system commands
    b) Avoiding hard-coded paths and environment variables
    c) Using only Bash syntax
    d) Running on Unix-based systems
  2. Which of the following should be avoided for portability in Shell scripting?
    a) Using absolute paths
    b) Using relative paths
    c) Using environment variables
    d) Using system commands
  3. How can you make a Shell script portable across different platforms?
    a) By avoiding complex logic
    b) By using only the /bin/bash interpreter
    c) By using POSIX-compliant syntax
    d) By specifying the absolute path for commands
  4. What is the benefit of writing portable Shell scripts?
    a) Improved system security
    b) Easier to debug
    c) Compatibility across different systems and platforms
    d) Faster execution
  5. Which of the following practices ensures portability in Shell scripts?
    a) Using $HOME for paths
    b) Using backticks for command substitution
    c) Avoiding the use of local variables
    d) Writing scripts for specific distributions
  6. How do you ensure that a Shell script runs correctly on different systems?
    a) Test the script on the target machine
    b) Include machine-specific commands
    c) Use non-standard packages
    d) Avoid any user input
  7. Which of the following is true when writing portable scripts?
    a) You should hard-code all paths
    b) The script must only work on Linux
    c) The script should use POSIX-compliant commands
    d) Use system-specific features for speed
  8. When writing portable Shell scripts, which interpreter should be specified?
    a) /usr/bin/bash
    b) /bin/sh
    c) /usr/bin/awk
    d) /bin/bash
  9. To write a portable script, which command should be used for creating directories?
    a) mkdir -p
    b) mkdir
    c) make
    d) cp
  10. Which of the following is a common mistake that reduces portability in Shell scripts?
    a) Writing clear comments
    b) Using echo to print messages
    c) Hard-coding file paths
    d) Using while loops

Packaging Scripts for Distribution

  1. What is a common method to distribute Shell scripts?
    a) Packaging them as executable files
    b) Storing them in a central repository
    c) Using email attachments
    d) Distributing them via social media
  2. Which file extension is commonly used for packaged Shell scripts?
    a) .sh
    b) .pkg
    c) .exe
    d) .tar.gz
  3. How can you package a Shell script with its dependencies for distribution?
    a) By compressing the script into a .tar.gz file
    b) By writing the script into a text file
    c) By compiling the script into an executable
    d) By using scp to transfer it
  4. Which command is commonly used to create a .tar.gz archive?
    a) tar -cvf
    b) tar -zcvf
    c) gzip
    d) compress
  5. What is the purpose of creating a .tar.gz package for a Shell script?
    a) To make the script portable and easy to distribute
    b) To protect the script from unauthorized access
    c) To speed up the execution of the script
    d) To ensure the script runs only on specific systems
  6. How can you ensure your Shell script is easy to install by others?
    a) Include installation instructions in the script itself
    b) Use a package manager for installation
    c) Encrypt the script for security
    d) Write an installer script in another language
  7. Which of the following is the best practice for packaging scripts for distribution?
    a) Including all dependencies in the same package
    b) Making the script a single executable file
    c) Writing complex installation instructions
    d) Avoiding the use of the tar command
  8. What is the advantage of using a .tar.gz file for packaging scripts?
    a) It makes the script secure
    b) It reduces the script’s size
    c) It ensures that the script cannot be modified
    d) It makes the script executable automatically
  9. When packaging a Shell script for distribution, which file should be included?
    a) A README.md file with usage instructions
    b) A config.h file with system configurations
    c) A .log file for error handling
    d) A .env file for environment variables
  10. How can you make a Shell script executable after packaging it for distribution?
    a) By adding the chmod +x command to the script
    b) By including a .exe extension
    c) By using tar to compress the script
    d) By converting it to a binary file

Documentation and Best Practices

  1. Why is documenting Shell scripts important?
    a) It makes the script easier to debug
    b) It helps other users understand and modify the script
    c) It speeds up the execution of the script
    d) It provides security for the script
  2. Which of the following is a good practice for documenting a Shell script?
    a) Writing no comments in the script
    b) Including a header with script description and usage instructions
    c) Using only one-line comments
    d) Avoiding comments for performance reasons
  3. What should a script header contain for proper documentation?
    a) Only the script’s name
    b) The purpose, author, and usage instructions
    c) The version of the operating system
    d) The specific commands used in the script
  4. Which of the following is a good practice when writing Shell scripts?
    a) Avoiding error handling
    b) Including meaningful variable names
    c) Using non-standard commands
    d) Making the script difficult to read
  5. What is the recommended approach for error handling in Shell scripts?
    a) Ignoring errors and continuing execution
    b) Using trap for catching signals and errors
    c) Using exit immediately after any error
    d) Logging errors to a file without notification
  6. How should you handle user input validation in Shell scripts?
    a) Use regular expressions or conditional statements
    b) Ignore user input
    c) Always assume input is correct
    d) Redirect input to a log file
  7. What is a common mistake when writing Shell scripts for distribution?
    a) Using standardized libraries
    b) Not including error handling
    c) Writing detailed comments
    d) Using portable command syntax
  8. Which command is used to make a script executable?
    a) chmod +x script.sh
    b) chmod -x script.sh
    c) chmod +r script.sh
    d) make executable script.sh
  9. What is the purpose of using set -e in a Shell script?
    a) To stop the script when an error occurs
    b) To increase the execution speed of the script
    c) To enable debugging in the script
    d) To set the script as executable
  10. How should you manage version control for your Shell scripts?
    a) By using a Git repository
    b) By keeping all versions in one script file
    c) By making the script read-only
    d) By using file timestamps

Answer Key

QnoAnswer
1b) Avoiding hard-coded paths and environment variables
2a) Using absolute paths
3c) By using POSIX-compliant syntax
4c) Compatibility across different systems and platforms
5a) Using $HOME for paths
6a) Test the script on the target machine
7c) The script should use POSIX-compliant commands
8b) /bin/sh
9a) mkdir -p
10c) Hard-coding file paths
11a) Packaging them as executable files
12a) .sh
13a) By compressing the script into a .tar.gz file
14b) tar -zcvf
15a) To make the script portable and easy to distribute
16b) Use a package manager for installation
17a) Including all dependencies in the same package
18b) It reduces the script’s size
19a) A README.md file with usage instructions
20a) By adding the chmod +x command to the script
21b) It helps other users understand and modify the script
22b) Including a header with script description and usage instructions
23b) The purpose, author, and usage instructions
24b) Including meaningful variable names
25b) Using trap for catching signals and errors
26a) Use regular expressions or conditional statements
27b) Not including error handling
28a) chmod +x script.sh
29a) To stop the script when an error occurs
30a) By using a Git repository

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