Bash

Bash 路徑補全如何與 sudo 一起使用?

  • June 28, 2015

選項卡路徑完成不起作用(至少在 Ubuntu 和 AFAIK Arch 上)

sudo mount <whatever>

我要掛載的 iso 文件不在/etc/fstab. 如果我只是輸入

mount <whatever>

完成工作(但當然命令失敗,因為我不是root)。顯然,它sudo打破了它。

如何使用 sudo 完成工作?

令人驚訝的是

sudo umount <whatever>

完成工程。它是如何實現的?它調查了/etc/fstab嗎?

**解決方案:**我只是將一個 shell 腳本放入帶有傳遞給它的參數的/usr/local/bin呼叫中。sudo mount ...呼叫此腳本時完成工作,因為沒有sudo障礙。

這一點都沒有關係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)
#

此外,您還可以在主文件/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.
#

更新

關於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.

發佈bash-completion 1.90

引用自:https://unix.stackexchange.com/questions/30827