开发者

while executing a shell script in Unix Bash Shell, how is a file location resolved for relative paths?

while executing a shell script in Unix Bash Shell, say any file in another folder and that is 开发者_如何学运维referenced by the script references some other file like ../../file_system_1/public/dir1/dir2/file2.xml

script.sh --> references dir1/file1 as "./dir1/file1" dir1/file1 references another file as ../../file_system_1/public/dir1/dir2/file2.xml

so relative to which file is the file2 resolved? to the script.sh location or the file1 folder location.


Relative paths are resolved in reference to the current working directory, as given by $(pwd).

Each time a resolution takes place, $(pwd) is evaluated. This means that the same ../myfile path string represents a different file before and after a cd command in your script.


In a shell script, .. pathnames are always relative to the current working directory, not to any other file pathname. this is the value of $PWD.


It depends on what you mean by "reference".

Relative paths are -- when used to access a file or directory -- interpreted relative to the current working directory of the process. The CWD is inherited across fork() and friends, so any sub-processes that your script executes will inherit the CWD. Interpreting relative paths is done by the C library and/or file system layer and this works the same in all programs that use the standard APIs, not just bash.

If your paths are not being used to access a file or folder, then they are just strings and do not "resolve" to anything.


What do you see, if you put an ls in your script.sh? That's the starting point for evaluating relative paths. Usually it's your current working directory. So to answer your question: It's probably neither script.sh's nor file1's location.

Why probably? Because it depends on what you do in your script. If you execute an cd /home/ in script.sh, the include path changes.


It's resolved relative to the current working directory. If the script doesn't change directories with cd or similar this is the directory in which you were when you started the script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜