smartsh/README.md

40 lines
1.4 KiB
Markdown
Raw Normal View History

2023-04-09 17:23:46 +00:00
smartsh.py is a simple python script that I wrote which can take any string as argument and queries OpenAPI's API to tell you how to accomplish the given task using a shell command. Yes, chatgpt from command line (sort of). But let me tell you a simple trick that you can use to supercharge your bash with it!
bash (I think starting from 4.0) conveniently provides a handler for situations when a command that the user entered is invalid.
You just need to provide a function named command_not_found_handle () and point it to the action to be taken on, yes, when a command is not found :-)
In this case, you need to add the following to your .bashrc for the magic to work:
2023-04-09 15:43:18 +00:00
```
export OPENAI_API_KEY="your_api_key"
2023-04-09 17:23:46 +00:00
export SMARTSH_PATH="path_where_you_checked_out_smartsh"
2023-04-09 15:43:18 +00:00
command_not_found_handle () {
echo "Let's get help from OpenAI API!"
python3 $SMARTSH_PATH/smartsh.py "$@"
}
2023-04-09 17:23:46 +00:00
```
Some examples:
```
bash$ Show the most recent file in the present working directory
Let's get help from OpenAI API!
ls -t | head -n1
```
```
bash$ Kill all Google chrome renderer processes
Let's get help from OpenAI API!
The command to accomplish this task is:
pkill -f "chrome.*renderer"
This command uses the `pkill` command to send a signal to all processes whose name matches the regular expression `chrome.*renderer`. This will effectively kill all Google Chrome renderer processes.
2023-04-09 15:43:18 +00:00
```