乐于分享
好东西不私藏

AIBash:如何用自然语言轻松生成Shell脚本命令?

AIBash:如何用自然语言轻松生成Shell脚本命令?

1. ShellGPT / AIBash 类工具
  • 这类工具(如 ShellGPT、AIBash)通常安装简单,配置好 API Key 后即可使用。它们能将你的自然语言描述直接转换成 Shell 命令。

  • 安装与配置:以 AIBash 为例,可以通过 pip 安装,首次运行时会引导你配置 OpenAI API Key 或本地模型(如 Ollama)。

pip install aibash-wx
aibash --init
2. AIBash配置
Select interface language (en/zh) [en]: zh #语言选择

请选择 AI 模型提供商:

1. OpenAI API 兼容服务(如 GPT、通义千问、Moonshot 等)

     Ollama(本地)
     请输入选项 (1/2 ):1# 模型选择,也可以选择本地模型。

API 基础地址 [https://api.openai.com/v1]:https://api.deepseek.com/v1 #模型URL地址

API Key(必填):sk-24b51c6eec434dae8c2e826 #API_key。

模型名称 [gpt-3.5-turbo]:deepseek-reasoner #模型名称
配置已保存到:/root/.aibash/config.yaml #配置文件
3. Deepseek模型名称
deepseek-chat 或 deepseek-reasoner
4. 使用帮助信息
[root@localhost ~]# aibash -h
usage: aibash [-h] [-l QUERY | -a QUERY | -A QUERY] [-p PATH] [--config PATH] [-new] [--auto-approve-all] [--auto-approve-commands] [--auto-approve-files] [--auto-approve-web] [--auto-max-steps AUTO_MAX_STEPS] [--ui-language {en,zh}] [-v] [--init] [--history] [--clear-history] [--test]

AI-powered shell command generator

options:
  -h, --help            show this help message and exit
  -l QUERY, --lang QUERY
                        Natural language description to generate a single shell command
  -a QUERY, --auto QUERY
                        Automation mode: describe the task in natural language to let AI plan and execute step by step
  -A QUERY, --analyze QUERY
                        Project analysis mode for large codebases; performs deep project inspection before planning actions
  --config PATH Specify config file path (default: ~/.aibash/config.yaml)
  -new, --new-terminal Execute generated commands in a new terminal window
  --auto-approve-all Automation: automatically approve all actions without confirmation
  --auto-approve-commands
                        Automation: automatically execute shell commands without confirmation
  --auto-approve-files Automation: automatically approve file reading actions
  --auto-approve-web Automation: automatically approve outbound web requests
  --auto-max-steps AUTO_MAX_STEPS
                        Automation: limit the maximum number of actions (default: 30)
  -p PATH, --plan-file PATH
                        Provide automation task description via file path
  --ui-language {en,zh}
                        Select UI language for this session (default from config)
  -v, --version show program's version number and exit
  --init Interactive configuration initialization
  --history View command execution history
  --clear-history Clear command execution history
  --test Test AI connection

Examples:
  aibash -l "list all files in current directory"
  aibash -l "find files containing test"
  aibash --config /path/to/config.yaml -l "perform some operation"

For more information, visit: https://github.com/W1412X/aibash
5. 示例
[root@localhost ~]# aibash -l "列出当前目录下的所有文件及目录"
正在生成命令...

========
生成的命令
========
  ls -la
========
请选择操作:
[e] 执行命令 [c] 复制到剪贴板 [m] 修改命令 [s] 跳过/取消 [h] 显示帮助
按键直接选择:

选择e键就能执行命令

6. shell脚本中使用
#!/bin/bash

# 尝试使用 AI 生成命令(假设 aibash 已修复)
if aibash -l "列出boot目录下的文件及目录" 2>/dev/null; then
echo"AI 生成成功"
else
# 如果 AI 失败,回退到默认命令
echo"AI 服务暂时不可用,使用默认命令..."
    ls -la /boot
fi

建议你先尝试使用 Ollama 本地模型,这样既快又稳定,完全避免了网络限流的问题。