Is there a smart pwd display program?

DaviD W. Sanderson dws at margay.cs.wisc.edu
Wed Apr 17 02:13:03 AEST 1991


In article <1991Apr15.170910.28160 at ux1.cso.uiuc.edu> jeffo at uiuc.edu writes:
>I'm writing a function for KSH that shows you your current working directory
>using tilde notation:
[description deleted]

This is what I use as part of my setup.  It doesn't decide how to print
things out; it just sets the variable PSD so you can use it elsewhere
however you like.  It is quite fast, since it only uses "case".

#-------
# setPSD - sets PSD, the part of my current directory path, to put
# into my prompt.
#
# There is a small point about testing for directories under $HOME.
# One should NOT simply check whether $PWD matches $HOME*, as this
# will not tell you if $PWD is your home directory or a directory
# under $HOME.  (If HOME=/var/home/dws, and PWD=/var/home/dwsalterego,
# this heuristic would tell you that you were in the dws account
# when you were not.)  Instead, one should check whether $PWD
# matches $HOME or $HOME/*, which is what this routine does.
#
# The number of "/*" in _setPSD_strip determines the maximum
# number of elements in the directory to be displayed after
# it is trimmed.
#
# DaviD W. Sanderson (dws at cs.wisc.edu)
#-------
_setPSD_strip="/*/*/*"	# tailor as desired.
setPSD()
{
	_wd="${1:-$PWD}"
	case $_wd
	{
	$HOME)
		#-------
		# An exact match of $HOME is the simplest case.
		#-------
		PSD="~"
		;;
	$HOME/*)
		#-------
		# A directory under $HOME.
		#-------
		_wd="${_wd#$HOME}"
		PSD0="${_wd%$_setPSD_strip/*}"
		PSD="${_wd#$PSD0/*/}"
		PSD="~${PSD}"
		;;
	*)	
		#-------
		# Show up to the last N directories in PATH, where
		# N is the number of "/*" in _setPSD_strip.
		# PSD begins with / for directories up to
		# N levels deep, no / for directories further down.
		#-------
		PSD0="${_wd%$_setPSD_strip/*}"
		PSD="${_wd#$PSD0/*/}"
		case $PSD
		{
		/*)	;;
		*)	PSD=" $PSD";;
		}
		;;
	}
	unset _wd PSD0
}
-- 
       ___
      / __\  U N S H I N E	           DaviD W. Sanderson
     |  |  | I N E			    dws at cs.wisc.edu
_____|  |  |_____  ________
\      / \   |__/ /////__	Fusion Powered Locomotives Made to Order
 \____/   \__|_/  \\\\\______	 (TARDIS model available at extra cost)



More information about the Comp.unix.shell mailing list