使用 ChatGPT AI 從英文文本生成 Linux 命令
即使是專家級(jí)的 Linux 用戶也不記得所有的 Linux 命令和它們的選項(xiàng)。這對(duì)我們?nèi)祟悂碚f是不可能的。
但是機(jī)器呢?尤其是人工智能驅(qū)動(dòng)的機(jī)器?
想象一下,如果你可以“命令”你的終端“顯示過去 12 小時(shí)內(nèi)修改過的所有小于 100 MB 的文件”。當(dāng)然,你可以使用 Linux 命令“命令”它,但是用普通的英語進(jìn)行交互呢?
由于人工智能的進(jìn)步,這實(shí)際上是可能的。下面是自動(dòng)生成 Linux 命令以顯示當(dāng)前目錄中所有小于 10 KB 的文件的示例。
Shell Genie AI assistent in Linux terminal
我使用的工具叫做 Shell Genie。它是一個(gè)命令行工具,可讓你以普通的英語與終端進(jìn)行交互。
它可以生成命令、運(yùn)行命令(如果需要),還可以向你解釋生成的命令。
Shell Genie explain commands
Shell-Genie 的特點(diǎn)
- 將普通英語轉(zhuǎn)換為 Linux 命令。
- 提供了一個(gè)需要 openAI 的 API 密鑰的 openAI gpt3 后端,和一個(gè)可以免費(fèi)使用的 free-genie 后端。
- 提示一個(gè)選項(xiàng)以運(yùn)行你要求的命令。
- 解釋生成的命令。
安裝 Shell Genie
Shell-genie 在任何發(fā)行版的默認(rèn)倉庫中都不可用。你可以使用 pipx
安裝它。
要安裝它,你需要安裝 Python 3.10+ 和 Pip。你可以參考我們關(guān)于 如何在 Ubuntu 和其他 Linux 發(fā)行版中安裝 pip 的文章。
安裝 pip
后,使用以下命令安裝 pipx
:
python3 -m pip install --user pipx
python3 -m pipx ensurepath
An SVG animation showing pipx Installation steps
現(xiàn)在,重啟終端并運(yùn)行以下命令安裝 shell-genie:
pipx install shell-genie
這可能顯示錯(cuò)誤或需要依賴項(xiàng)。
A dependency installation to install the shell-geie properly
運(yùn)行提示的命令來安裝所需的依賴。在我的例子中:
sudo apt install python3.10-venv
之后,再次運(yùn)行 shell-genie
安裝命令,就可以安裝了。
The steps showing the installation of shell-genie
安裝完成后,運(yùn)行以下命令:
shell-genie init
這將要求你選擇后端,openAI 或 free-genie。如果你有 openAI API,你可以選擇它或繼續(xù)使用 free-genie。
?? free-genie 后端可能并不總是工作,因?yàn)樗怯砷_發(fā)者托管的,他警告說可能會(huì)出現(xiàn)中斷。
然后它將請(qǐng)求允許報(bào)告反饋。用 y/n
來決定。
Run shell-genie init commad to set up the shell-genie properly
現(xiàn)在就可以使用了。
使用 Shell-genie
?? 如果你要進(jìn)行實(shí)驗(yàn),請(qǐng)盡量不要使用帶有
sudo
或刪除文件的命令。不要將你的機(jī)器交到機(jī)器手中。
如上所述,shell-genie 提供了兩種工作模式:
- 從普通的英語獲取命令
- 獲取命令解釋
從普通英語中獲取 Linux 命令
你可以使用 shell-genie 的 ask
選項(xiàng)從普通的英語中獲取命令。例如:
shell-genie ask "Display only the folders of this directory"
這將顯示正確的命令,并提示我們是否運(yùn)行該命令。
The working of shell-genie, that will print the required command from the provided plain text description. Also propt the user to execute the same or not
獲取帶解釋的 Linux 命令
你可以使用 shell genie 來解釋你要運(yùn)行的一些命令。
shell-genie ask "display all files smaller than 10kb in here" --explain
上面的命令首先會(huì)顯示需要的命令并進(jìn)行解釋,然后提示用戶是否執(zhí)行。
The explain mode in shell-genie, where it will explain the command that was asked in the form of plain english and then prompts the user to execute it or not
總結(jié)
有像 Explain Shell 這樣的工具(試圖)解釋 Linux 命令。但是這個(gè) Shell genie 通過從普通的英語生成命令將它提升到一個(gè)新的水平。
當(dāng)然,不能一味依賴人工智能。如果你對(duì) Linux 命令有一定的了解,可以使用 Shell Genie 生成適合你的命令。你不必為手冊(cè)頁或各種網(wǎng)站而苦惱。
它可以幫助你在終端中更快地做事,也可以減少你的知識(shí)儲(chǔ)備。為什么? 因?yàn)槟阍揭蕾囁?,你自己學(xué)的就越少。
(題圖:MJ/chatgpt commands linux cli illustration in high resolution, very detailed, 8k)