🎄How to get help with shell commands using AI / chatgpt/copilot🌟

Dec 13 2023

Back to 2023 Advent Calendar

Bash scripts can be confusing and hard to remember even for experienced developers.

For example, I always forget how to find out what listens to a specific port, or how do I find heavy files on my disk.

There are also many domain specific tools that have even more complicated set of flags like ffmpeg.

Today we'll try 2 different tools:

  1. github copilot CLI (https://docs.github.com/en/copilot/github-copilot-in-the-cli/using-github-copilot-in-the-cli)
  2. shell-gpt (https://github.com/TheR1D/shell_gpt)

there are many more, but these 2 are the ones that I've tried.

Copilot CLI

$ gh copilot suggest "find out what listens on the port 3000"

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

? What kind of command can I help you with?
> generic shell command

Suggestion:                                                                                                                                                                                                  
                                                                                                                                                                                                             
  lsof -i :3000                                                                                                                                                                                              

? Select an option
> Copy command to clipboard

the answer is correct, however it does a lot of chatting and asking for confirmations and choosing options using arrow keys.

the trick is to use a flag

gh copilot suggest "convert static image + mp3 to video using ffmpeg" --target  shell

Welcome to GitHub Copilot in the CLI!
version 0.5.3-beta (2023-11-09)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

Suggestion:                                                                                                                                                                                                  
                                                                                                                                                                                                             
  ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:v libx264 -c:a aac -shortest output.mp4                                                                                                                        

? Select an option  [Use arrows to move, type to filter]

that is better.

and FFMPEG question I was also able to provide the answer.

 gh copilot suggest "convert static image + mp3 to video using ffmpeg" --target  shell

Welcome to GitHub Copilot in the CLI!
version 0.5.3-beta (2023-11-09)

I'm powered by AI, so surprises and mistakes are possible. Make sure to verify any generated code or suggestions, and share feedback so that we can learn and improve.

Suggestion:                                                                                                                                                                                                  
                                                                                                                                                                                                             
  ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:v libx264 -c:a aac -shortest output.mp4                                                                                                                        

? Select an option  [Use arrows to move, type to filter]
> Copy command to clipboard

Shell GPT

To get the keys

to get API keys go to https://platform.openai.com/api-keys , you might have to create account first. Then create new keys and copy them to your clipboard. Having separate keys for each project is a good idea, so you can revoke them later if needed.

Installation

pls follow up to date instructions on https://github.com/TheR1D/shell_gpt

Running it

sgpt "what listens to port 3000"
To find out what is listening on port 3000, you can use the following command in your zsh shell:

  
   lsof -i :3000


This command will show you the process or application that is currently using port 3000.

That basically looks like ChatGPT response that was asked with custom prompt.

for FFMPEG:

sgpt "convert static image + mp3 to video using ffmpeg"
To convert a static image and an MP3 file into a video using ffmpeg, you can use the following command:

    ffmpeg -loop 1 -i image.jpg -i audio.mp3 -c:v libx264 -tune stillimage -c:a aac -b:a 192k -pix_fmt yuv420p -shortest output.mp4

that looks correct.

In conclusion, there are many of such command line tools

sgpt uses your OpenAI key, while CoPilot uses their own subscription.

sgpt does provider more custom options and is more flexible.

sgpt --help                                                                          
                                                                                                                                                                                                             
 Usage: sgpt [OPTIONS] [PROMPT]                                                                                                                                                                              
                                                                                                                                                                                                             
╭─ Arguments ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│   prompt      [PROMPT]  The prompt to generate completions for.                                                                                                                                           │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Options ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --model                             TEXT                       Large language model to use. [default: gpt-3.5-turbo]                                                                                      │
│ --temperature                       FLOAT RANGE [0.0<=x<=2.0]  Randomness of generated output. [default: 0.1]                                                                                             │
│ --top-probability                   FLOAT RANGE [0.1<=x<=1.0]  Limits highest probable tokens (words). [default: 1.0]                                                                                     │
│ --editor             --no-editor                               Open $EDITOR to provide a prompt. [default: no-editor]                                                                                     │
│ --cache              --no-cache                                Cache completion results. [default: cache]                                                                                                 │
│ --help                                                         Show this message and exit.                                                                                                                │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Assistance Options ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --shell           -s                 Generate and execute shell commands.                                                                                                                                 │
│ --describe-shell  -d                 Describe a shell command.                                                                                                                                            │
│ --code                --no-code      Generate only code. [default: no-code]                                                                                                                               │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Chat Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --chat                             TEXT  Follow conversation with id, use "temp" for quick session. [default: None]                                                                                       │
│ --repl                             TEXT  Start a REPL (Read–eval–print loop) session. [default: None]                                                                                                     │
│ --show-chat                        TEXT  Show all messages from provided chat id. [default: None]                                                                                                         │
│ --list-chats    --no-list-chats          List all existing chat ids. [default: no-list-chats]                                                                                                             │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
╭─ Role Options ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ --role                              TEXT  System role for GPT model. [default: None]                                                                                                                      │
│ --create-role                       TEXT  Create role. [default: None]                                                                                                                                    │
│ --show-role                         TEXT  Show role. [default: None]                                                                                                                                      │
│ --list-roles     --no-list-roles          List roles. [default: no-list-roles]                                                                                                                            │
╰───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

while copilot only has 2 options:

 gh copilot suggest --help                                                           
Suggest a command based on a natural language description of the desired output effect.

There is no task too small or too large to need help when working in the terminal, the only limit is your imagination:

- installing and upgrading software
- troubleshooting and debugging problems with your system
- processing and manipulating files
- working with git and GitHub in the terminal

Usage:
  copilot suggest [flags]

Examples:

- Guided experience
  $ gh copilot suggest

- Git use cases
  $ gh copilot suggest -t git "Undo the most recent local commits" 
  $ gh copilot suggest -t git "Clean up local branches" 
  $ gh copilot suggest -t git "Setup LFS for images" 

- Working with the GitHub CLI in the terminal
  $ gh copilot suggest -t gh "Create pull request"
  $ gh copilot suggest -t gh "List pull requests waiting for my review"
  $ gh copilot suggest -t gh "Summarize work I have done in issues and pull requests for promotion"

- General use cases
  $ gh copilot suggest -t shell "Kill processes holding onto deleted files"
  $ gh copilot suggest -t shell "Test whether there are SSL/TLS issues with github.com"
  $ gh copilot suggest -t shell "Convert SVG to PNG and resize"
  $ gh copilot suggest -t shell "Convert MOV to animated PNG"