. dot 调用sh提示找不到文件 - bash POSIX

【问题场景描述】

Linux系统替换;

脚本未变,原机器可执行成功;

#!/usr/bin/sh

cd ${WORKPATH}
. test.sh         # 报错test.sh cannot found,实际${WORKPATH}下存在

【原因】

. filename [arguments]

source filename [arguments] Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename. If filename does not contain a slash, file names in PATH are used to find the directory containing filename. The file searched for in PATH need not be executable. When bash is not in posix mode, the current directory is searched if no file is found in PATH. If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched. If any arguments are supplied, they become the positional parameters when filename is executed. Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

翻译一下:

如果文件名未包含 / , 使用环境变量PATH去查找;

当bash不在posix模式下时,才会查询当前目录。(推测不同Linux版本,不同的模式)

https://stackoverflow.com/questions/32778192/filename-cant-find-file-when-run-from-bin-sh-works-from-bin-bash-why

【解决】

export PATH=.:$PATH
或
. ./test.sh

【附录】

POSIX 怎么就被开启了?

1. sh一般设成bash的软链

lrwxrwxrwx. 1 root root 4 Jun 25 10:52 /usr/bin/sh -> bash

2. 在一般的linux系统当中,使用sh调用执行脚本相当于打开了bash的POSIX标准模式

3. 也就是说 /bin/sh 相当于 /bin/bash --posix

 

posted @ 2019-12-03 19:27  QA小虾  阅读(391)  评论(0编辑  收藏  举报