Skip to content

bash script to instantiate chatgpt and generate commands to complete a text-based description right in the command line.

Notifications You must be signed in to change notification settings

trackzero/chatbash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 

Repository files navigation

ChatBash

ChatBash is a Bash script that interacts with OpenAI's GPT models to generate and execute shell commands based on user input. It ensures that dangerous commands are identified and prompts the user for confirmation before execution.

Prerequisites

Before using ChatBash, make sure you have the following installed on your system:

  • Bash (Tested on Bash 4.4+)
  • curl
  • jq

Installing Dependencies

You can install the required dependencies using the following command:

sudo apt-get update && sudo apt-get install -y curl jq

Installation

  1. Clone the repository:

    git clone https://github.com/trackzero/chatbash.git
    cd chatbash
  2. Set up your OpenAI API Key:

    Export your OpenAI API key as an environment variable:

    export OPENAI_API_KEY="your-api-key-here"

    Alternatively, you can add the above line to your .bashrc or .bash_profile to persist the environment variable across sessions.

  3. Make the script executable:

    chmod +x chatbash

Usage

Run the script with a description of what you want to accomplish:

./chatbash "Description of the task you want to accomplish"

Alternatively, you can run the script without any arguments, and it will prompt you for a description:

./chatbash

The script will generate shell commands based on the provided description using OpenAI's GPT4o model. It will then display the commands and ask for your confirmation before executing them.

Example

./chatbash.sh "List all files in the current directory and count them"

but why does it....

Fine, you want to make it executable as a regular shell command?

sudo ln -s ~/chatbash/chatbash /usr/local/bin/chatbash  #adjust source directory as appropriate.

Safety Features

  • The script checks for potentially dangerous commands (e.g., rm -rf, mkfs, etc.) and warns the user before executing them.
  • It prompts the user for confirmation before executing any commands.
  • I'm pretty lazy when it comes to error checking and safety features so there's probably a lot I missed. Use at your own risk. Make backups first. You're on your own if you break something. And for the love of whatever gods you prefer, do not run this on prod systems.

Example use

ChatBashDev:~$ chatbash "Can you tell me what version of linux Im running, what version of python is installed, and what version of node is installed?"
Commands to be executed:
uname -a
python --version
node --version
Do you want to execute these commands? (yes/no): yes

Executing commands...
Running: uname -a
Linux genai 6.8.8-2-pve #1 SMP PREEMPT_DYNAMIC PMX 6.8.8-2 (2024-06-24T09:00Z) x86_64 x86_64 x86_64 GNU/Linux
Running: python --version
Python 3.12.4
Running: node --version
/usr/local/bin/chatbash: line 106: node: command not found
Error: Command 'node --version' failed with status 127.
Execution will continue with the next command.

But I thought you worked for...

Yeah yeah, I know. There's a Bedrock version, too. That's the chatbash-br version.

But there's some twists. You'll need two environment variables:

export BEDROCK_API_KEY="your-key-here"
export BEDROCK_API_URL="http://<your-bedrock-access-gateway-url-here>/api/v1"

"But I don't have a bedrock access gateway!" you say.

Well, there's one over here: https://github.com/aws-samples/bedrock-access-gateway

Setup of the gateway is beyond the scope of this readme. Could I have done it natively? Probably. Did I? Well, obviously not.

About

bash script to instantiate chatgpt and generate commands to complete a text-based description right in the command line.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages