Bash 函数名称与系统二进制文件冲突

sch*_*ity 0 bash shell-script

在我的脚本中,我有一个名为messages. 我在 Linux Mint 中编写它,运行它没有问题,当我将它移到 Debian Buster 工作站时,该功能与/usr/bin/messages.

我有一个调用脚本的启动脚本messages

启动脚本

# call to messages script
. messages
Run Code Online (Sandbox Code Playgroud)

消息

messages() {
  # reformat the arguments and return them
}
Run Code Online (Sandbox Code Playgroud)

稍后在 startup_script

messages "This is a message"
Run Code Online (Sandbox Code Playgroud)

哪个抛出

./startup_script: line 35: .: /usr/bin/messages: cannot execute binary file
messages: could not open mailbox `/path/to/my/script/<string passed to my function>': No such file or directory
Run Code Online (Sandbox Code Playgroud)

所以我得到了一堆与/usr/bin/messages被调用而不是我的函数相关的错误。

添加后type messages "This is a message",相关输出为:

messages is /usr/bin/messages
Run Code Online (Sandbox Code Playgroud)

我可以选择重命名我的函数¹,但也许有更好的方法来处理这种情况。

如何告诉我的脚本忽略系统二进制文件并使用我自己的函数?


¹ 该函数在多个脚本中多次调用,因此仅更改名称并不是最简单的选择。

Kam*_*ski 5

这是. file工作原理:

如果file不包含 <slash>,shell 将使用 指定的搜索路径PATH来查找包含文件的目录。

此行为由 POSIX 指定

你的第一个错误是

./startup_script: line 35: .: /usr/bin/messages: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)

当你打电话时它是相似的. echo

-bash: .: /bin/echo: cannot execute binary file
Run Code Online (Sandbox Code Playgroud)

您正在尝试获取二进制文件/usr/bin/messages。实际上,您定义该函数的文件根本没有来源,该函数未在当前脚本中定义。这意味着以后messages仍然意味着/usr/bin/messages,而不是功能。

有关行应该是. ./messages. /full/path/to/messages(即路径文件,而不是二进制)。