I have to use the older ksh on SUN and AIX alot...
and kinda missed the pushd concept for jumping around a bunch of directories.
but not really, because I didnt always want to pop off my directory, just wanted to cd there.
So I wrote a ksh or bash friendly script with usage as below.
Make it available by sourcing: . godir.sh
Code:
# Script: godir
# Written: 05-16-11 13:49:17 By: AWJurgensen
# Implements "go" utility. go to a directory maintained from an array
# go is a cd/pushd/popd replacement, that saves your paths for future go's
# environment variable GODIR contains all directory entries
# EDIT flds array definition below for your shell: ksh or bash
# LOAD it up by sourcing into your shell: . godir.sh
GOUSAGE='Example usage of the go command (a cd/pushd/popd/dirs alternative):
go list the GODIR directory array
go 2 goto directory entry 2
go -3 remove directory entry 3
go -! remove all directory entries
go -? show this usage help
go newdir cd to newdir, creating new entry
go 9:newdir cd to newdir, creating/updating entry 9
go w:/work cd to work, creating/updating entry w'
# this is the array in var=val:var=val format
#GODIR=0=/full/path:1=/path
typeset GODIR
export GODIR
# Usage: Array {add|del|get|dump|exists|arrayadd} [pair]
# pair is in format: var=value
function GoArray {
#set -x
typeset cmd=${1} pair=${2}
typeset -i add=0 delete=0 get=0 dump=0 debug=0
case $cmd in
add) add=1 ;;
del*) delete=1 ;;
get) get=1 ;;
dump) dump=1 ;;
exists) if [ "$(echo "$GODIR" |grep ${pair}=)" != "" ] ; then
return 0
else
return 1
fi ;;
arrayadd) if [ "$GODIR" != "" ] ; then
GODIR="$GODIR:$pair"
else
GODIR="$pair"
fi
return ;;
*) echo "Array: Unsupported action $cmd"
return ;;
esac
typeset OIFS IFS field var value flds
typeset -i err=0 i=0 numflds=0
((debug)) && print -u2 "pair: $pair"
OIFS=$IFS
IFS=:
set -A flds -- ${GODIR} # ksh only; load array flds
# flds=(${GODIR}) # bash only; load array flds
IFS=$OIFS
numflds=${#flds[*]}
if [[ add -gt 0 ]] ; then
add=numflds
GoArray arrayadd "$add=$pair"
return
fi
GODIR=""
# loop thru all N=/path entries:
while [[ i -lt numflds ]]; do
field=${flds[i]}
IFS==
set -- $field
IFS=$OIFS
var=${1}
value=${2}
if [[ delete -gt 0 && $var = "$pair" ]] ; then
((debug)) && print -u2 "deleting: $pair"
elif [[ get -gt 0 && $var = "$pair" ]] ; then
echo "$value"
GoArray arrayadd "$field"
elif [[ dump -gt 0 ]] ; then
printf "%-2.2s %s\n" $var "$value"
GoArray arrayadd "$field"
# Add is handled before the loop
#elif [[ add -gt 0 -a add -eq i ] ; then
# GoArray arrayadd "$add=$pair"
else
GoArray arrayadd "$field"
fi
((i+=1))
done
}
# go is a cd replacement, that stores your directories for future go's
function go
{
NUL=/dev/null
#set -x
case "$1" in
-!) GODIR="" ;;
-\?) echo "$GOUSAGE" ;;
-*) GoArray del ${1#-}
GoArray dump | sort ;;
*:*) lbl=${1%:*} # remove pattern on right
dir=${1#*:} # remove pattern on left
if [ -d "$dir" ] ; then
cd "$dir" >$NUL ; echo go: $(/bin/pwd)
# Check if were replacing an entry. then do delete, add:
GoArray exists ${lbl} && GoArray del ${lbl}
GoArray arrayadd "${lbl}=$(/bin/pwd)"
else
GoArray dump | sort
fi ;;
*) if [ -d "$1" ] ; then
cd "$1" >$NUL ; echo go: $(/bin/pwd)
GoArray add "$(/bin/pwd)"
else
GO=$(GoArray get "$1")
if [ -d "$GO" ] ; then
cd "$GO" >$NUL ; echo go: $(/bin/pwd)
else
GoArray dump | sort
fi
fi ;;
esac
}
Here's what I added to .profile to "seed" GODIR with my favorite directories:
Code:
# load go function (a cd alternative)
. $HOME/bin/godir.sh
# load up our favorite directories for use by: go
GODIR='0=/home/alan:b=/home/alan/bin:d=/home/alan/doc:s=/home/alan/shells:p=/home/alan/perl:w=/work:m=/data/media/mp3:l=/var/log:t=/tmp'
alias g='cd' # go home
alias b='cd -' # go back to previous dir