It is currently Tue, 07 Dec 2021 01:36:14 GMT



 
Author Message
 Need fix for short ksh script

The following ksh script for cd with a dir stack broke when we upgraded
to Solaris 2.5.1 from 2.3.  I did not change the script which I have in
my .kshrc, and, as far as I can tell, the system manager has not
changed anything relevant.  When things are working properly, cd is used
to change directories and cdd brings up a directory list from which a previous
dir can be selected.  However, now when I use cdd, if a selected directory
has a leading ~/ in it (e.g., ~/whatever), cdd bombs saying that directory
/myhome~/whatever does not exist (ie., ~ is expanded and tacked on front),
except when the selected directory is item 1 in the directory list.

I'm not a ksh programmer and would appreciate any help you can give me.
Thanks very much.

Steve

Dr. Stephen C. Piper
Geosciences Research Division
Scripps Institution of Oceanography A-020
La Jolla, CA 92093-0220

tel: (619) 534-4230 ext. 12
fax: (619) 534-8814
email: pi...@cdrgsun.ucsd.edu

#
# From Bolsky and Korn's KSH book, Pg. 247
#
#
# DIRECTORY MANIPULATION FUNCTIONS, REPLACES CD
#
# Uses global variables _push_max , _push_top , _push_stack
#

integer _push_max=${CDSTACK-20} _push_top=${CDSTACK-20}
unalias cd
alias cd=_cd

# Display directory stack -- ${HOME} displayed as ~

function dirs
{
        typeset dir="${PWD#${HOME}/}"

        case ${dir} in
        ${HOME})
                dir=\~
                ;;
        /*)     ;;
        *)      dir=\~/${dir}
                ;;
        esac

        PS3=
        select i in "${dir}" "${_push_stack[@]}"
        do
                :
        done < /dev/null

# Change directory and put directory on front of stack

function _cd
{
        typeset dir=
        integer n=0 type=4

        case $1 in
        -|-1|2)                 # same as cd -
                n=_push_top
                type=1
                ;;
        -[1-9]|-[1-9][0-9])     # cd -n
                n=_push_top+${1#-}-1
                type=2
                ;;
        1)                      # stay put
                \print -r - "${PWD}"
                return
                ;;
        [2-9]|[1-9][0-9])       # cd n
                n=_push_top+${1}-2
                type=2
                ;;
        *)      if ((_push_top <= 0))
                then
                        n=_push_max
                        type=3
                fi
                ;;
        esac

        if ((type<3))
        then
                if ((n >= _push_max))
                then
                        \print cd: Directory stack not that deep!
                        return 1
                else
                        dir=${_push_stack[n]}
                fi
        fi

        case ${dir} in
        ~*)     dir=${HOME}${dir#~}
                ;;
        esac

        \cd "${dir:-${@}}" > /dev/null || return 1
        dir=${OLDPWD#${HOME}/}

        case $dir in
        ${HOME})
                dir=\~
                ;;
        /*)     ;;
        *)      dir=\~/$dir
                ;;
        esac

        case $type in
        1)      # swap first two elements
                _push_stack[_push_top]=$dir
                ;;
        2|3)    # put $dir on top and shift down by one until top
                integer i=_push_top
                for dir in "$dir" "${_push_stack[@]}"
                do
                        ((i > n)) && break
                        _push_stack[i]=$dir
                        i=i+1
                done
                ;;
        4)      # push name
                _push_stack[_push_top=_push_top-1]=$dir
                ;;
        esac

        \print -r - "${PWD}"

# Menu-driven change directory command

function cdd
{
        typeset dir="${PWD#${HOME}/}"

        case $dir in
        ${HOME})
                dir=\~
                ;;
        /*)     ;;
        *)      dir=\~/$dir
                ;;
        esac

        PS3='Select by number or enter a name: '
        select dir in "$dir" "${_push_stack[@]}"
        do
                if _cd $REPLY
                then
                        return
                fi
        done
#end



 Sun, 28 Nov 1999 03:00:00 GMT   
 Need fix for short ksh script

Save yourself the hassle and install GNU bash which has built-in directory
stacks.



 Sat, 04 Dec 1999 03:00:00 GMT   
 
   [ 2 post ] 

Similar Threads

1. Need reference translate ksh script to bash

2. Help needed with basic .ksh script logic

3. Newbie needs a little help on shell scripts (ksh)

4. Need ksh or Unix script now!

5. Need semi-non-trivial example of an (MKS) ksh script [really programming-by-example]

6. Need Help with KSH script running in TCSH

7. need help with ksh awk/ftp script

8. Need help writing script in ksh

9. help needed for running cu from ksh script


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software