jsdy@hadron.UUCP (Joseph S. D. Yao) (12/27/85)
OK, not a bad idea: a program to generate your favourite searchpath for your favourite type of person. You say your syntax is: searchpath type=Xxxxxx ? Q&D: alias searchpath "grep \"`echo $1 | sed 's/type=//'`[ ^I]\" /etc/paths | line | sed \"s/^$1[ ^I]*//\"" I probably messed up some of the quoting, since I have not tried this. If you don't have the Korn shell (or C shell), or this doesn't work, or you want better checking: #! /bin/sh # # @(#)searchpath.sh 1.1 # PATH="/bin:/usr/bin"; export PATH # Defaults. Note no "." or user's bin -- in a system shell # script, that is n o t a good idea. defpath="/bin:/usr/bin:/usr/lbin:/usr/local/bin" deftype="user" # Where it is, man. pathfile="/etc/paths" # Input and local variables. type="$1" path="" # Get the type, unadorned with "type=", which violated # ANSI standard argument sequence anyway. ;-) if [ "" = "$type" ]; then type="$deftype" else # Note: case "") doesn't work in some states. ;-) case "$type" in type=*) type="`echo "$type" | sed 's/^type=//'`" ;; esac fi # Find that type of path in the paths file. path="`grep \"^$type[ ^I]\" $pathfile | line | sed \"s/^$type[ ^I]*//\"`" # "Dear me, I'm still not certain quite # that even now I've got it right." -- E. Lear # (Mild testing indicates this should work, tho.) # If no such type, complain and set to default. if [ "" = "$path" ]; then echo "$0: No such type in $pathfile as \"$type\"" >&2 path="$defpath" fi # Output the path name. echo "$path" exit 0 And of course /etc/paths contains something like: user /bin:/usr/bin:/usr/lbin:/usr/local/bin systems /etc:/bin:/usr/bin:/usr/lbin:/usr/local/bin acctg /usr/acctg/bin:/bin:/usr/bin:/usr/lbin:/usr/local/bin and so forth. -- Joe Yao hadron!jsdy@seismo.{CSS.GOV,ARPA,UUCP}
idallen@watmath.UUCP (01/02/86)
People who advocate setting the PATH variable and using "only" relative path names in shell scripts are forgetting all the absolute path names that they put in the PATH variable. Just as commands move from search directory to search directory, so do search directories change, either over time or among host machines. You can't use hard-coded path names there, either! We have several different UNIX systems going here, with the result that the choice of paths in the PATH variable is not the same on all machines. Beyond /bin and /usr/bin, which exist on every UNIX machine we've ever heard of, the writer of a shell script destined for distribution to all our machines does not know where a particular machine is keeping a command; hence, s/he cannot hard-code a particular set of directories at the start of a system shell script. The choices of distributing different versions of shell scripts to different machines or making the shell scripts set their PATH by checking which machine they are on both suffer from having the path information for a machine replicated in the scripts. If a machine changes search paths, all the scripts have to be found and changed. So, Waterloo took the leap and wrote a command, /bin/searchpath, whose primary function is to return the list of standard search paths when executed on a particular host. Using arguments, the command can also return a list of paths suitable for use by "system maintainers", people doing "accounting", etc. From the user's point of view, s/he selects the type of commands s/he needs by category, and /bin/searchpath supplies the places where they reside on the current host. The command provides useful detail-hiding, since the pathnames themselves are usually anachronistic and irrelevant to shell-script programmers. We create and maintain a file on each host that has the search path information for that host, for use by our command. "Porting" our shell scripts to a new UNIX machine involves creating the file(s) containing the search path information for that host. New shell scripts now start with a command to set the PATH variable: PATH="`/bin/searchpath type=users`"; export PATH These scripts work on all our UNIX systems, no matter how odd the search path structure is at that moment. We have a category of search path for systems people, and it includes the "test" path(s) where we put commands that we want to test before they get installed for real. We are free to add, delete, and rename paths to suit our file system space or program organization needs without requiring anyone (especially overworked systems people) to change a single line of code. All our new users are supplied with login profiles that set $PATH using /bin/searchpath. So, our shell scripts only need to know *one* absolute pathname -- /bin/searchpath. The rest is done dynamically according to what kind of search paths are needed and what host the script is running on. The command requires a bit of overhead at the start of a shell script, but, gosh, it sure makes life easier. -- -IAN! (Ian! D. Allen) University of Waterloo