linux – “. filename“从 binsh运行时找不到文件,从 binbash工作;为什么?

linux – “. filename“从 binsh运行时找不到文件,从 binbash工作;为什么?,第1张

概述我的简单脚本是这样的: #!/bin/shDEF=.file_name_with_a_leading_dot.sh. ${DEF} 注意顶行的/ bin / sh. 当我运行这个简单的脚本时,我收到一个错误,找不到该文件. 但是,如果我将该顶行更改为#!/ bin / bash,则脚本会在当前目录中找到该文件. 但是,在我的Ubuntu linux笔记本电脑上,我看到/ bin / sh 我的简单脚本是这样的:
#!/bin/shDEF=.file_name_with_a_leading_dot.sh. ${DEF}

注意顶行的/ bin / sh.
当我运行这个简单的脚本时,我收到一个错误,找不到该文件.
但是,如果我将该顶行更改为#!/ bin / bash,则脚本会在当前目录中找到该文件.

但是,在我的Ubuntu linux笔记本电脑上,我看到/ bin / sh是/ bin / bash的符号链接.那么,为什么我的脚本表现不同?

另外,我可以像这样运行脚本:

/bin/bash ./script.sh

没关系.

那么,我错过了什么?

解决方法 从 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. 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.

所以,似乎shebang#!/ bin / sh将你的bash设置为posix模式.在此模式下,仅评估PATH,而不是当前目录.

总结

以上是内存溢出为你收集整理的linux – “. filename“从/ bin / sh运行时找不到文件,从/ bin / bash工作;为什么?全部内容,希望文章能够帮你解决linux – “. filename“从/ bin / sh运行时找不到文件,从/ bin / bash工作;为什么?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1049324.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存