Bash 路径补全如何与 sudo 配合使用?

Ali*_*Ali 11 bash sudo path mount autocomplete

选项卡路径完成不起作用(至少在 Ubuntu 和 AFAIK Arch 上)与

sudo mount <whatever>
Run Code Online (Sandbox Code Playgroud)

我尝试挂载的 iso 文件不在/etc/fstab. 如果我只是输入

mount <whatever>
Run Code Online (Sandbox Code Playgroud)

完成工作(但当然命令失败,因为我不是 root)。显然是sudo它打破了它。

如何使用 sudo 完成工作?

令人惊讶的是,与

sudo umount <whatever>
Run Code Online (Sandbox Code Playgroud)

完成工作。它是如何实现的?它调查/etc/fstab吗?

解决方案:我只是将一个 shell 脚本放入带有传递给它的参数的/usr/local/bin调用中sudo mount ...。调用此脚本时完成工作,因为没有sudo障碍。

enz*_*tib 11

这完全没有关系bash,但它取决于包中编程的完成bash-completion

从文件中的一些评论/etc/bash_completion.d/mount

# mount(8) completion. This will pull a list of possible mounts out of
# /etc/{,v}fstab, unless the word being completed contains a ':', which
# would indicate the specification of an NFS server. In that case, we
# query the server for a list of all available exports and complete on
# that instead.
#

# umount(8) completion. This relies on the mount point being the third
# space-delimited field in the output of mount(8)
#
Run Code Online (Sandbox Code Playgroud)

此外,您可以在主文件中找到/etc/bash_completion以下注释,明确讨论mountumount命令:

# A meta-command completion function for commands like sudo(8), which need to
# first complete on a command, then complete according to that command's own
# completion definition - currently not quite foolproof (e.g. mount and umount
# don't work properly), but still quite useful.
#
Run Code Online (Sandbox Code Playgroud)

更新
有关的评论mountumount取出命令,从bash_completion提交

_command_offset: Restore compopts used by called command.

This fixes completions that rely on their compopts, most notably
mount(8).
Fixes bash-completion bug #313183.
Run Code Online (Sandbox Code Playgroud)

发布bash-completion 1.90