Skip to content

Instantly share code, notes, and snippets.

@knee-cola
Last active December 18, 2024 08:55
Show Gist options
  • Save knee-cola/40c41e5bc3cf9679b5167729c3c9663c to your computer and use it in GitHub Desktop.
Save knee-cola/40c41e5bc3cf9679b5167729c3c9663c to your computer and use it in GitHub Desktop.
#!/bin/bash
# ============================================================
# Script: gpt - Automate Bash Command Suggestions with GPT-4
# ============================================================
# This script generates Bash commands using OpenAI's GPT-4 API
# based on user-provided prompts and copies the result to the
# clipboard using xclip for immediate use.
#
# Key Features:
# - OpenAI GPT-4 Integration: Generates Bash commands from prompts.
# - Clipboard Automation: Copies the generated command to the clipboard via xclip.
# - Alias Setup: Configures an alias (`gpt`) in .bashrc for quick and easy usage.
# - PATH Management: Ensures the script's directory is in the user's PATH.
# - Dependency Check: Validates required tools (xclip and jq).
# - User-Friendly Error Handling: Provides clear instructions for missing dependencies or prompts.
#
# Prerequisites:
# - OpenAI API Key: Replace "your-open-ai-api-key-goes-here" with your API key.
# - xclip Installed: Install using "sudo apt install xclip".
# - jq Installed: Install using "sudo apt install jq".
#
# Usage:
# 1. Place the script in a directory on your system.
# 2. Add your OpenAI API key in the API_KEY variable.
# 3. Run the script with a prompt:
# gpt Find all .log files in the current directory
# 4. The generated command will be printed and copied to your clipboard.
#
# Example:
# Input Prompt:
# gpt List all files larger than 100MB
# Output Command:
# find . -type f -size +100M
#
# The output will be copied to your clipboard, ready for pasting.
#
# Keywords: Bash GPT-4 integration, OpenAI CLI tool, Shell scripting,
# Clipboard automation, xclip, .bashrc alias setup
# ============================================================
API_KEY="your-open-ai-api-key-goes-here"
# Path to this script
SCRIPT_PATH="$(realpath "$0")"
SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
ALIAS_NAME="gpt"
# Check if the script's directory is in PATH
if ! echo "$PATH" | grep -q "$SCRIPT_PATH"; then
echo "The script's file path ($SCRIPT_PATH) is not in PATH. Adding it to .bashrc..."
echo "export PATH=\$PATH:$SCRIPT_PATH" >> ~/.bashrc
echo "Added $SCRIPT_PATH to PATH. Run 'source ~/.bashrc' to apply changes."
fi
# Check if alias is defined in .bashrc
if ! grep -q "alias $ALIAS_NAME=" ~/.bashrc; then
echo "Alias '$ALIAS_NAME' is not defined in .bashrc. Adding it..."
echo "alias $ALIAS_NAME=$SCRIPT_PATH" >> ~/.bashrc
echo "Alias '$ALIAS_NAME' added to .bashrc. Run 'source ~/.bashrc' to apply changes."
fi
# Check if xclip is installed
if ! command -v xclip &> /dev/null; then
echo "xclip is not installed. Install it using: sudo apt install xclip"
exit 1
fi
PROMPT="$*"
if [ -z "$PROMPT" ]; then
echo "Usage: gpt <your prompt>"
exit 1
fi
# Make the API call
COMMAND=$(curl -s https://api.openai.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $API_KEY" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "system", "content": "You are a helpful assistant that only responds with bash commands that achieve the requested operation."},
{"role": "user", "content": "'"$PROMPT"'"}
]
}' | jq -r '.choices[0].message.content')
# Print the command for the user
echo "Use the following command (paste it from clipboard):"
echo "$COMMAND"
# Copy to clipboard
echo "$COMMAND" | xclip -selection clipboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment