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
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
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
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
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
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
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
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
When writing portable Shell scripts, which interpreter should be specified? a) /usr/bin/bash b) /bin/sh c) /usr/bin/awk d) /bin/bash
To write a portable script, which command should be used for creating directories? a) mkdir -p b) mkdir c) make d) cp
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
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
Which file extension is commonly used for packaged Shell scripts? a) .sh b) .pkg c) .exe d) .tar.gz
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
Which command is commonly used to create a .tar.gz archive? a) tar -cvf b) tar -zcvf c) gzip d) compress
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Qno
Answer
1
b) Avoiding hard-coded paths and environment variables
2
a) Using absolute paths
3
c) By using POSIX-compliant syntax
4
c) Compatibility across different systems and platforms
5
a) Using $HOME for paths
6
a) Test the script on the target machine
7
c) The script should use POSIX-compliant commands
8
b) /bin/sh
9
a) mkdir -p
10
c) Hard-coding file paths
11
a) Packaging them as executable files
12
a) .sh
13
a) By compressing the script into a .tar.gz file
14
b) tar -zcvf
15
a) To make the script portable and easy to distribute
16
b) Use a package manager for installation
17
a) Including all dependencies in the same package
18
b) It reduces the script’s size
19
a) A README.md file with usage instructions
20
a) By adding the chmod +x command to the script
21
b) It helps other users understand and modify the script
22
b) Including a header with script description and usage instructions
23
b) The purpose, author, and usage instructions
24
b) Including meaningful variable names
25
b) Using trap for catching signals and errors
26
a) Use regular expressions or conditional statements