如果您尝试运行未安装的软件包,例如:
me ~: gparted
The program 'gparted' is currently not installed. You can install it by typing:
sudo apt-get install gparted
Run Code Online (Sandbox Code Playgroud)
如何将该sudo apt-get install gparted
行作为命令运行?不打字,显然。
您可以通过将其添加到您的 .bashrc
export COMMAND_NOT_FOUND_INSTALL_PROMPT=1
Run Code Online (Sandbox Code Playgroud)
给你:
$ foo
The program 'foo' is currently not installed. You can install it by typing:
sudo apt-get install blah-blah
Do you want to install it? (N/y)
Run Code Online (Sandbox Code Playgroud)
如果您收到如下 Python 错误:
...
File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 217, in install_prompt
answer = answer.decode(sys.stdin.encoding)
AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)
你可以:
从这里为 Ubuntu应用此补丁。
或:通过在第 215,216,217 行开头添加四个空格进行
修改(注意:不是制表符):CommandNotFound.py
... 213 其他: 第214话 2154 spaces 如果 sys.stdin.encoding 和 isinstance(answer, str): 216 4 spaces # 解码答案,以便我们得到一个 unicode 值 第2174 spaces 话 218 如果 answer.lower() == _("y"): ...
另一种方式,扩展Greg Hewgill的评论可能是:
$(!! 2>&1 | tail -n1)
Run Code Online (Sandbox Code Playgroud)
我试图将它包装在一个脚本中,但这并没有立即生效,因为您不会收到错误消息。
如果想在脚本中获取它,一种相当笨拙的方法可能是:
x=$(/usr/lib/command-not-found $(tail -n1 "$HOME/.bash_history") 2>&1 | tail -n1)
echo "$x"
Run Code Online (Sandbox Code Playgroud)
对于一些选项,请查看:
/usr/lib/command-not-found -help
Run Code Online (Sandbox Code Playgroud)
和/或检查脚本本身。