#!/bin/ksh # # ncd clone # # Partially based in: # Fergal Goggin 11-April-2000, SSL ncd clone # # Create a list of directories with find and store in .ncd.MACHINENAME # Zgrep the file each time user requests the change dir # # Compare requested dir name with each $line##*/ from grep to see if it matches even partially and store # # check if current dir is in the list of matched and jump to the next in the list # if not currently in any of the dirs in the list, jump to the first in the list # # # Don't forget to create an alias, otherwise you won't change anywhere: # alias go='. go' # # Versions # V1.0 2008/07/18 Search based in the beginning of the directory name and with a second file # to implement the rotation along the existing dirs that start with the same # pattern # # V1.1 2008/07/21 Fixed a bug where a pattern belonging to the middle of the directory name # causes noLines to be non-zero and later $NCDTMPROTFILE not being created, # since the pattern is not at the begin of the directory name, although it # exists! # # V1.2 2008/07/22 Removed all TABS # Fixed +($1*) to $1* to work also in bash # There is still a variable visibility problem in bash at lines 109, 118 #treat unset variables as if they were set # and errors like "ksh: @: parameter not set" go away set +u BOLD_TYPE="" NORMAL_TYPE="" INVERSE_TYPE="" if [ $TERM = "xterm" ] then BOLD_TYPE="" NORMAL_TYPE="" INVERSE_TYPE="" fi NCDDATABASE=$HOME/.ncd.`uname -n` NCDTMPFILE=/tmp/$$.ncd NCDTMPROTFILE=/tmp/$$rot.ncd if [ "$BASEDIR" = "" ] then BASEDIR=$HOME fi if [ ! -f $NCDDATABASE ] then echo "re-creating database from $BASEDIR" find $BASEDIR -type d 2> /dev/null | gzip > $NCDDATABASE elif [ $# -lt 1 ] then echo "go, change directory for UNIX, fgoggin 2000 --- based on ncd, by fgoggin 2000" echo "usage: go - change directory to " echo "usage: [BASEDIR=XXX] go - recreate database (delete .ncd.* in case of trouble) " else zgrep $1 $NCDDATABASE > $NCDTMPFILE trap 'rm -f $NCDTMPFILE $NCDTMPROTFILE' 2 noLines=0 # wc -l seems to stick a tab in front of it output # so it is just as easy to use grep -c noLines=`grep -c ^ $NCDTMPFILE` if [ $noLines -eq 0 ] then echo "No match" elif [ $noLines -eq 1 ] then # exact match cd `cat $NCDTMPFILE` else # search for those whose basename contain $1 partially or as a whole rm -f $NCDTMPROTFILE cat $NCDTMPFILE | { while read LINE; do BASENAME=${LINE##*/}; #if [[ $BASENAME = +($1*) ]]; # nice! if basename equals something that has 1 or more times $1 if [[ $BASENAME = $1* ]]; # nice! if basename equals something that has 1 or more times $1 then #echo MATCH: $LINE; echo $LINE >> $NCDTMPROTFILE fi done } #cat $NCDTMPROTFILE # sometimes the user inserts a pattern that occurs in the middle of the directory name # and that causes noLines to be non-zero. # Since the previous if (the one with the nice! comment) only catches directories that # start with the pattern, this file may not be created! and so that problem is prevented here! if [ ! -f $NCDTMPROTFILE ]; then echo "No match" rm -f $NCDTMPFILE; return 1; fi # check if the pwd is any of the list and if it is jump to the next CHOICE=1 DESTINATION="" DESTINATIONROTATION="" CWD=`pwd` cat $NCDTMPROTFILE | { while read LINE; do #echo Processing: $LINE # keep the first to use by default if [ $CHOICE -eq 1 ]; then DESTINATION=$LINE DESTINATIONROTATION=$LINE fi if [[ "$LINE" = "$CWD" ]]; then echo Multiple choices for [$1] and you are at ${INVERSE_TYPE}${LINE}${NORMAL_TYPE}; read DESTINATION; break; fi CHOICE=`expr $CHOICE + 1` #echo $CHOICE done } # at the end of the list, it should rotate to the home dir if [ "$DESTINATION" = "" ]; then DESTINATION=$DESTINATIONROTATION fi echo "switching to $DESTINATION"; cd $DESTINATION fi fi rm -f $NCDTMPFILE $NCDTMPROTFILE unset CHOICE unset DESTINATION unset DESTINATIONROTATION unset CWD unset NCDDATABASE unset NCDTMPFILE unset NCDTMPROTFILE unset NORMAL_TYPE unset INVERSE_TYPE unset BOLD_TYPE unset noLines BASEDIR=""