Unix dot command

折月煮酒 提交于 2019-12-11 05:09:34

问题


I have the following code snippet, with my question below. As i am completely new to unix, i am not even sure what i should be google searching for to begin with.

I know that ./Path is to run a particular program in the current path I know also that . /PATH is to run a program in another directory.

But what about . ./PATH ?

if [[ -f ./dbatools.pro ]]; then
        . ./dbatools.pro  -> **what does this do ? I don’t think such a command is possible ?** 
else
        . /app/dbatools/profile/dbatools.pro
fi

echo "Started at `date`"

回答1:


The dot . command is standard POSIX shell (Bourne, Korn, Bash) notation for 'read the named file as if it was part of the current script'. The big advantage is that the file can set environment variables and define functions and it affects the shell script. Normally, if you run the script as a regular command (using ./dbatools.pro or sh dbatools.pro), then the environment variables affect only the shell that executes the script, not the current shell.

The test looks to see if there is a file called dbatools.pro in the current directory (hence ./dbatools.pro). If there is, it uses that file; if there is not, then it uses the file /app/dbatools/profile/dbatools.pro. It will generate an error if it cannot read the file it 'dots'.

With Bash, there's an alternative notation, source ./dbatools.pro, that can be used instead. It is borrowed from the C shell.

Note that the . (and source, a Bash builtin) commands will search for a plain file (. dbatools.pro, for example) in a directory on $PATH, but the file does not need to be executable — it only needs to be readable.



来源:https://stackoverflow.com/questions/12398149/unix-dot-command

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