我有以下设置
??? modules
??? main.sh
??? major.sh
??? sub-modules
? ??? minor.sh
Run Code Online (Sandbox Code Playgroud)
/modules$ PATH的路径.all.sh:
source major.sh
Run Code Online (Sandbox Code Playgroud)
major.sh:
source sub-module/minor.sh
Run Code Online (Sandbox Code Playgroud)
但是,当我运行这个时,我得到:
-bash: /sub-module/minor.sh: No such file or directory
Run Code Online (Sandbox Code Playgroud)
是的。这就是./source运算符的工作原理。
从规格来看:
shell 应在当前环境中执行文件中的命令。
如果 file 不包含 <slash>,shell 将使用PATH指定的搜索路径来查找包含file的目录。然而,与普通命令搜索不同的是,点实用程序搜索的文件不需要是可执行的。如果没有找到可读文件,非交互式 shell 将中止;交互式 shell 应将诊断消息写入标准错误,但这种情况不应被视为语法错误。
因此,当您使用source file.shshell 时,会在 中进行搜索,$PATH但是当您使用时,source dir/file.sh查找$PATH会被跳过,并被dir假定为相对于当前目录。
要执行此类操作(在main.sh或内部major.sh),您可以使用Bash 脚本能否告诉它存储在哪个目录中?找到合适的绝对路径来使用。
对于一般用途,您需要将它们全部插入$PATH或source将它们从绝对/等等中插入。路径。