can't source script in a current directory

本小妞迷上赌 提交于 2019-11-30 03:24:15

问题


So apparently, I can't source a script if that script is in the current directory. For example,

# source some/dir/script.sh
Ok

works fine, but if I'm in the same dir as the script, it errors out:

# cd some/dir
# source script.sh
-sh: source: script.sh: file not found

What gives? Is the only way around this to change directory?

I'm using bash v4.2.10 on Angstrom Linux if that's relevant.


回答1:


Quoting the source man page:

source filename [arguments]

....

If filename does not contain a slash, file names in PATH are used to find the directory containing file- name.

So... source is trying to search your script.sh in the folders contained in PATH.

If you want to source a file in the current folder use

source ./script.sh



回答2:


Use an absolute path -- source /root/path/to/some/dir/script.sh -- should sort you.




回答3:


This can happen when the file is in the wrong format. I FTP'd a Korn Shell script from Windows. I could edit it, but got "not found [No such file or directory]" when I tried to run it. It turned out it was in DOS format, which was indicated in the file name line when I edited it in vi. After I re-FTP'd it, making sure it was being transferred as ASCII, it ran fine.



来源:https://stackoverflow.com/questions/14889921/cant-source-script-in-a-current-directory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!