简体   繁体   English

".filename" 从 /bin/sh 运行时找不到文件,从 /bin/bash 运行; 为什么?

[英]". filename" can't find file when run from /bin/sh, works from /bin/bash; why?

My simple script is like this:我的简单脚本是这样的:

#!/bin/sh


DEF=.file_name_with_a_leading_dot.sh

. ${DEF}

Notice the /bin/sh on the top line.注意最上面一行的 /bin/sh。 When I run that simple script I get an error that the file isn't found.当我运行那个简单的脚本时,我得到一个找不到文件的错误。 But, if I change that top line to #!/bin/bash then the script finds that file in the current directory just fine.但是,如果我将第一行更改为 #!/bin/bash,那么脚本会在当前目录中找到该文件就可以了。

But, On my Ubuntu linux laptop I see that /bin/sh is a symlink to /bin/bash .但是,在我的 Ubuntu linux 笔记本电脑上,我看到 /bin/sh 是 /bin/bash 的符号链接。 So, why does my script behave differently?那么,为什么我的脚本表现不同呢?

Also, I can run the script like this:另外,我可以像这样运行脚本:

/bin/bash ./script.sh

And it's OK.没关系。

So, what am I missing?那么,我错过了什么?

From the Manpage :手册页

. . 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. source filename [arguments]在当前 shell 环境中从 filename 读取并执行命令,并返回从 filename 执行的最后一个命令的退出状态。 If filename does not contain a slash, file names in PATH are used to find the directory containing filename.如果文件名不包含斜杠,则使用 PATH 中的文件名来查找包含文件名的目录。 The file searched for in PATH need not be executable.在 PATH 中搜索的文件不必是可执行文件。 When bash is not in posix mode, the current directory is searched if no file is found in PATH.当 bash 不处于 posix 模式时,如果在 PATH 中没有找到文件,则搜索当前目录。 If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.如果 shopt 内置命令的 sourcepath 选项关闭,则不会搜索 PATH。 If any arguments are supplied, they become the positional parameters when filename is executed.如果提供了任何参数,它们将在执行 filename 时成为位置参数。 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.返回状态是脚本中退出的最后一个命令的状态(如果没有执行任何命令,则返回 0),如果找不到文件名或无法读取文件名,则返回 false。

So, it seems that the shebang #!/bin/sh sets your bash to posix mode.因此,shebang #!/bin/sh似乎将您的 bash 设置为 posix 模式。 In this mode, only PATH is evaluated, not the current directory.在这种模式下,只评估PATH ,而不是当前目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法从 bin 文件夹运行 java - Can't run java from bin folder /bin/sh: [./文件名]: 未找到 - /bin/sh: [./Filename]: not found /bin/sh 替代 /bin/bash 中的 ${!#} - /bin/sh alternative to ${!#} in /bin/bash 从execv启动终端(sh)返回错误:/ bin / sh:/ bin / sh:无法执行二进制文件 - start terminal (sh) from execv return error : /bin/sh: /bin/sh: cannot execute binary file 为什么我不能运行文件箱中的文件? - Why can't I run the file located in the bin? 为什么execve系统调用运行“/ bin / sh”而没有任何argv参数,而不是“/ bin / ls”? - Why can the execve system call run “/bin/sh” without any argv arguments, but not “/bin/ls”? /bin/sh VS. /bin/bash - /bin/sh VS. /bin/bash 如何从 /bin/sh 判断文件是否早于 30 分钟? - How can I tell if a file is older than 30 minutes from /bin/sh? “ / bin / bash cd〜”显示为“ / bin / bash:cd:没有这样的文件或目录”,为什么? - “/bin/bash cd ~” results in “/bin/bash: cd: No such file or directory”, why? 从Cron作业执行bash脚本时出现“ / bin / bash ^ M:错误的解释器:没有这样的文件或目录”错误 - “/bin/bash^M: bad interpreter: No such file or directory” error when executing a bash script from a cron job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM