Skip to content

Instantly share code, notes, and snippets.

@MohamedElashri
Created September 15, 2024 01:25
Show Gist options
  • Select an option

  • Save MohamedElashri/986aaea60b16ab900df54a2761bc2cb6 to your computer and use it in GitHub Desktop.

Select an option

Save MohamedElashri/986aaea60b16ab900df54a2761bc2cb6 to your computer and use it in GitHub Desktop.
LLM shell script generation prompt helper

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:

  1. Comprehensive Error Handling and Input Validation:

    • Use set -euo pipefail at 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.
  2. 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.
  3. 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 -x but offers more control over the output.
  4. Strategic Error Handling with set -e and set +e:

    • Enable set -e to exit immediately on errors.
    • Use set +e strategically 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
  5. 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
  6. 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
  7. Usage Instructions:

    • Provide a --help option that displays detailed usage information and examples.
  8. Comments and Clear Naming:

    • Use descriptive variable and function names.
    • Include comments to explain complex sections of the code.
  9. Modularity:

    • Organize the script into functions to improve readability and maintainability.
  10. 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment