【发布时间】:2019-04-05 20:50:55
【问题描述】:
MVCE:
关于bash 4.1.2、Centos 6.10(因为我们是怪物)。
sudo command vim
返回
sudo: command: command not found
我不明白为什么。在bash 4.4.23、Mac OS High Sierra 上,它按预期工作。由于人们使用“命令”作为他们正在谈论的各种命令的占位符,因此很难在 google 上搜索。
实际问题:
我有一个名为 vim 的函数(见下文),将 command 从它对 vim 的调用中取出 不会 导致它失败,就像我所期望的那样,在两者上bash 4.4 和 4.1。
如果我无法写入文件,我有一个自动 sudo vim 的功能:
vim() {
#only good for auto-sudo. delete if no sudo privileges.
if [[ "$#" -ne 1 ]]; then
command vim "$@"
#cases: if we can write to the file, or the file doesn't exist but we can make new files in that dir
elif [[ -w "$1" || ( -w $(dirname "$1") && ! -f "$1" ) ]]; then
# \vim or 'vim' only escape aliases, not functions
command vim "$1"
else
#this 'command' isn't required! It won't loop forever without it.
sudo env HOME="$HOME" command vim -u $HOME/.vim/vimrc "$1"
fi
}
我希望命令是必需的,否则vim 应该引用我创建的函数并将其自身称为无限。但是,不仅在 CentOS 和 Mac 系统上不需要,而且它还会导致 CentOS 机器上的功能失败!
谁能解释这种行为?
是否有一个方便的 bash 更改日志我可以查看以了解“命令”是否在 bash 4.1 之后才以某种方式实现?
【问题讨论】: