Absolute vs. Relative paths: we use *one* absolute path here

Joseph S. D. Yao jsdy at hadron.UUCP
Sat Dec 28 05:41:42 AEST 1985


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 at seismo.{CSS.GOV,ARPA,UUCP}



More information about the Comp.unix.wizards mailing list