Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uwm.edu!rutgers!uwvax!margay.cs.wisc.edu!dws From: dws@margay.cs.wisc.edu (DaviD W. Sanderson) Newsgroups: comp.unix.shell Subject: Re: Is there a smart pwd display program? Keywords: smart pwd display tilde-notation fast Message-ID: <1991Apr16.161303.14566@spool.cs.wisc.edu> Date: 16 Apr 91 16:13:03 GMT References: <1991Apr15.170910.28160@ux1.cso.uiuc.edu> Sender: news@spool.cs.wisc.edu (The News) Organization: U of Wisconsin CS Dept Lines: 73 In article <1991Apr15.170910.28160@ux1.cso.uiuc.edu> jeffo@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@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@cs.wisc.edu _____| | |_____ ________ \ / \ |__/ /////__ Fusion Powered Locomotives Made to Order \____/ \__|_/ \\\\\______ (TARDIS model available at extra cost)