#!/bin/bash
# Copyright (C) 1999 Ronald Ira Feigenblatt. All Rights Reserved.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

thisname=`basename $0`

function EXPLAIN {
	cat >&2 <<- HID

	Usage: $thisname [-h|--help] [starting path]	
	Function: Interactively find another directory path, but ONLY print the name.
	 This console utility provides some of the function of a file manager tool by
	 letting you use a menu hierarchy to avoid typing long directory path names.
	 The name 'monkey' suggests climbing up/down the file tree.
	Version: 0.3 (alpha)
	History: 0.2 (99.01.09), 0.3 (99.03.21)
	Dependencies: /bin/bash; grep; tr
	Tested platforms: Microsoft Windows 98(TM) (Cygnus Cygwin Beta 20.1), Linux RH 5.0
	Author: Ron Feigenblatt
	License: Free Software Foundation, GNU General Public License, version 2

	Details: All output appears on STANDARD ERROR, EXCEPT for the name
	 of the FINAL directory path, which is printed out to STANDARD OUTPUT.
	 The "current directory" of execution remains UNCHANGED in the end. The 
	 current directory path is used as a starting point if none is specified.

	Caution: Because Windows(TM) paths can EMBED spaces, the final output
	 from this utility is single-quoted. All the same,
	 while this works:	eval "cd \`monkey\`"
	 and this works:			DIR=\`monkey\`; eval "cd \$DIR"
	 this fails to work as one might expect:		cd \`monkey\`
	 because back-quoting then strips the embracing single quotes.

	HID
	exit -1
}

function ONEDIR {
	if [ $# -gt 1 ]; then echo "${thisname}: Only ONE starting path can be specified" >&2; EXPLAIN; fi;
}

OPTIONS=""
DIRS=""
for ARG
do
    case "$ARG" in
	--help|-h)	EXPLAIN;;
	-f)		FILEMODE=1;
			echo "${thisname}: Option -f not yet implemented" >&2;; # file handling not yet implemented
	-*)		OPTIONS="$OPTIONS $ARG";;
	*)		if test -d "$ARG"; then
            		DIRS="$DIRS $ARG"
        		else
            		echo "${thisname}: $ARG not a legitimate starting directory" >&2; EXPLAIN;
        		fi ;;
    esac
done
if [ $OPTIONS ]; then echo "${thisname}: $OPTIONS is/are illegal options" >&2; EXPLAIN; fi;
eval "ONEDIR `echo $DIRS`"
cd ${DIRS:-.}

# Unlike Cygwin, Linux version of 'select' issues PS3 to stdout
monkeyPS3=${PS3}
PS3=

while true; do true;
echo -e "\n" >&2
echo "Current directory contents are:" >&2
ls -al >&2
echo "Current directory is `pwd`" >&2
#select X in `ls -ap1 | grep "/" | tr "/" " " `
echo "Enter 0 to end navigation in current directory" >&2
#exit
select X in `ls -ap1 | grep "/" | tr " " "\260" | tr "/" " "`
do true; if [ $REPLY -eq 0 ]; then break 2; fi;
#	echo -e "\"$X\"" >&2
#	echo -e "\"$X\"" | tr "\260" " " >&2
eval "cd \"`echo $X | tr "\260" " "`\"";
break; done;
done
echo "'`pwd`'"

PS3=${monkeyPS3}
monkeyPS3=

    Source: geocities.com/siliconvalley/ridge/6664

               ( geocities.com/siliconvalley/ridge)                   ( geocities.com/siliconvalley)