我的简单脚本是这样的:
#!/bin/sh
DEF=.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,而不是当前目录.
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|