inputrc 中的 if 语句

yzf*_*fr1 5 readline inputrc

我为我的 bashrc 使用一个中心位置,并为我的所有用户提供这样的源:

GLOBAL_BASHRC="/path/to/global/bashrc"

if [ -r "$GLOBAL_BASHRC" -a -f "$GLOBAL_BASHRC" ]; then
    # Load the bashrc
    source $GLOBAL_BASHRC
else
    echo "Warning: Bashrc file [${GLOBAL_BASHRC}] does not exist!"
fi
Run Code Online (Sandbox Code Playgroud)

现在我想对我的 inputrc 文件做同样的事情。我已经从这个问题中找到了如何在 inputrc 中执行等效于 source 的操作:

$include /path/to/global/inputrc
Run Code Online (Sandbox Code Playgroud)

但问题是,就像在上面的 bashrc 中一样,我想要一些错误处理,我只想加载实际存在的文件。因此,我的问题是,如何指定 if 语句检查输入文件中是否存在文件?

meu*_*euh 4

您不需要$include从您的~/.inputrc,因为您可以随时读取 inputrc 文件

bind -f /path/to/global/inputrc
Run Code Online (Sandbox Code Playgroud)

因此,请使用您常用的if [ -r file ]this 而不是source.


的手册页显示,对于交互式登录 shell,它读取/etc/profile 并第一个找到~/.bash_profile~/.bash_login~/.profile。对于其他交互式 shell,它读取~/.bashrc,对于非交互式 shell,它读取文件$BASH_ENV(如果有)。

在您的情况下,您可能正在使用终端仿真器的非登录交互式 shell,因此~/.bashrc可以读取。strace您可以使用和 虚拟 home来查看会发生什么,例如在 中/tmp

$ touch /tmp/.inputrc /tmp/.bashrc
$ (unset BASH_ENV INPUTRC HISTFILE
   HOME=/tmp strace -e open -o /tmp/out bash -i)
Run Code Online (Sandbox Code Playgroud)

这显示了正在打开的以下文件(为了简洁起见,我删除了一些文件):

open("/tmp/.bashrc", O_RDONLY)          = 3
open("/tmp/.bash_history", O_RDONLY)    = -1 ENOENT
open("/tmp/.inputrc", O_RDONLY)         = 3
open("/tmp/.bash_history", O_WRONLY|O_CREAT|O_TRUNC, 0600) = 3
Run Code Online (Sandbox Code Playgroud)

所以 bash 会.inputrc在 . 之后读取.bashrcINPUTRC这是有道理的,因为它让您有时间在文件中进行设置。