When generating the shell script, please ensure it adheres to the following best practices to provide a robust and user-friendly experience on Unix/BSD systems:
-
Comprehensive Error Handling and Input Validation:
- Use
set -euo pipefailat the beginning of the script to catch errors, unset variables, and failed pipes. - Validate all input arguments and provide meaningful error messages to guide the user.
- Use
-
Clear and Colorful Output:
- Implement clear, user-friendly messages.
- Use color coding (e.g., bold yellow for warnings) to enhance readability and draw attention to important information.
-
Detailed Progress Reporting:
- Include a function to print each command before executing it for transparency and easier debugging:
function print_and_execute() { echo "+ $@" >&2 "$@" }
- This mirrors the output of Bash's
set -xbut offers more control over the output.
- Include a function to print each command before executing it for transparency and easier debugging:
-
Strategic Error Handling with
set -eandset +e:- Enable
set -eto exit immediately on errors. - Use
set +estrategically within loops or sections where you want the script to continue despite individual command failures.set -eo pipefail for item in "$@"; do set +e # Commands that may fail but shouldn't exit the script set -e done
- Enable
-
Platform-Specific Adaptations:
- Detect the operating system and adjust script behavior to ensure consistency across different environments:
if [ "$(uname -s)" == "Linux" ]; then TIMEOUT="timeout -v $RUN_TIME_LIMIT" else # Assume macOS if [ -x "$(command -v gtimeout)" ]; then TIMEOUT="gtimeout -v $RUN_TIME_LIMIT" else echo -e "${BOLD_YELLOW}WARNING${RESET} gtimeout not available. Install with \`brew install coreutils\`." fi fi
- Detect the operating system and adjust script behavior to ensure consistency across different environments:
-
Timestamped File Outputs for Multiple Runs:
- Implement timestamping to prevent overwriting results and maintain a history:
filetimestamp=$(date +"%Y%m%d%H%M%S") # Use $filetimestamp in your output file names
- Implement timestamping to prevent overwriting results and maintain a history:
-
Usage Instructions:
- Provide a
--helpoption that displays detailed usage information and examples.
- Provide a
-
Comments and Clear Naming:
- Use descriptive variable and function names.
- Include comments to explain complex sections of the code.
-
Modularity:
- Organize the script into functions to improve readability and maintainability.
-
Enhanced User Experience:
- Handle errors gracefully without exposing unnecessary technical details.
- Avoid assumptions about the user's environment; check for required tools and dependencies, and inform the user if they're missing.