#!/bin/ksh ############################################## ## Prints abridged working directory path in form "head...tail" ## Usage: PWD [TEXT] ## TEXT The string to abridge (defaults to current path, $PWD or `pwd`) ## $PWD_HEAD Show this many leading characters from given text (default 10) ## $PWD_TAIL Show this many trailing characters from given text (default 20) ## ## Attempts to tilde syntax for home directories, like $HOME/../user = ~user ## Great for use in $PS1, like: export PS1='\u@\h $(PWD \w)\$ ' ## ## PWD 2.1, Copyright (c) 2002-8 by Adam Katz , GPL ############################################## [ "$1" = "--help" ] && \ grep '^## ' `which $0 2>/dev/null || echo $0` |sed 's/^...//' && exit # defaults a=10 b=20 c=33 [ -n "$*" ] && TEXT="$*" || TEXT="$PWD" # ksh does links wrong; use bash or $1 [ -z "$TEXT" ] && TEXT=`pwd` if [ -n "$PWD_TAIL$PWD_HEAD" ]; then [ -n "$PWD_HEAD" ] && expr $PWD_HEAD + 1 >/dev/null 2>&1 && a="$PWD_HEAD" [ -n "$PWD_TAIL" ] && expr $PWD_TAIL + 1 >/dev/null 2>&1 && b="$PWD_TAIL" c="$(expr $a + $b + 3)" fi # set up other users' home directories # zsh actually does this already, only translate real paths, need to read passwd if [ "$SHELL" != "/bin/zsh" ] && [ -r "$TEXT" ] && [ -r /etc/passwd ]; then for hdir in `grep '/bin/..*sh$' /etc/passwd |awk -F ':' '{printf $6":"$1" "}'` ":$HOME" do echo "$hdir # $tusr # $thom # $tpat" >/tmp/pwd tusr="${hdir%:*}" thom="${hdir#*:}" tpat="${TEXT#$thom}" if [ "$TEXT" != "$tpat" ]; then TEXT="~$tusr$tpat"; fi done fi # verify length to ensure head and tail don't repeat anything, then abridge [ "$(echo $TEXT|wc -c)" -gt "$c" ] && \ TEXT=`echo $TEXT|sed -e "s:^.\{$a\}:&///:" -e "s:.\{$b\}$:///&:" -e "s:///.*///:...:"` # b=`expr $b + 1` TEXT="`echo $TEXT|head -c$a`...`echo $TEXT|tail -c$b`" echo $TEXT