Something similar to PATH environment variable - but for non-executable files?

Something similar to PATH environment variable - but for non-executable files?

Post by Greg Dharm » Tue, 10 Feb 2009 21:18:44


Hi all

I am aware that when we fire a command at the shell prompt, unix looks
in all the directories in the the PATH environment variable and runs
the first filename matching the command name in the directories in the
PATH in order...

I was wondering if there is something similar for non-executable files
as well.

For example, if I have a file foo.txt and I do
$ cat foo.txt
Is it possible in any way for foo.txt to be searched for in a list of
directories and for the first match to be typed out?

Thanks, would appreciate any pointers...

~Greg.
 
 
 

Something similar to PATH environment variable - but for non-executable files?

Post by gordonb.6v » Wed, 11 Feb 2009 04:51:34

>I am aware that when we fire a command at the shell prompt, unix looks

It's possible to code such a thing (this, of course, requires having
source code to the command doing the search. You cannot reasonably
do this in the shell alone as the shell does not know what is a
file and what isn't). C compilers often have search paths for
include files and a separate one for libraries. And sometimes
one for pieces of the compiler also.

I can see some useful functions like
FILE *fopen_by_path(char *filename, char *path, char *mode);
and
char *find_file_by_path(char *filename, char *path);


Things get a LOT messier when you consider what should happen
when the file is an *output* file or is to be created. Or deleted.
What should:
cat foo.txt
rm foo.txt
cat foo.txt
rm foo.txt
cat foo.txt > foo.txt
do?


There is something to be said for not trying to make the whole
directory structure a single gigantic directory. Search paths are
useful, but they are more useful when you can set a search path for
a specific type of file (and "non-executable" isn't nearly specific
enough). C compilers work nicely with separate paths for include
files and libraries. Some Java implementations use CLASSPATH for
classes. A database search path for a file-based database might
work well.

 
 
 

Something similar to PATH environment variable - but for non-executable files?

Post by Stephane C » Wed, 11 Feb 2009 19:24:54

2009-02-9, 04:18(-08), Greg Dharma:
[...]

With very recent versions of zsh, you could do:

typeset -T FILEPATH filepath
zsh_directory_name() {
[[ $1 = n ]] || return
local i
for i in "$filepath[@]"; do
[[ -f $i/$2 ]] || continue
reply=($i/$2)
return 0
done
return 1
}


Then:

~$ FILEPATH=:~/tmp:/tmp
~$ echo foo > /tmp/foo.txt
~$ cat ~[foo.txt]
foo
~$ echo ~[foo.txt]
/tmp/foo.txt
~$ cat ~[bar.txt]
zsh: no directory expansion: ~[bar.txt]


(yes, ~[...] is meant to expand directories, but you can use
that for anything, that looks like a bit of a dodgy design if
you ask me).

--
Sthane