These Korn shell functions implement the C shell directory management commands: dirs, popd, and pushd. They can be put into a separate cshfuncs file and read in with the . command when necessary. Their basic functionality is: dirs | display directory stack | popd | remove directory stack entry and cd to it | pushd | add directory stack entry and cd to it | The dirs function lists the current directory stack. With no argument, the popd function removes the top entry (previous working directory) from the directory stack and changes directory to it. If a +n argument is given, then the nth directory stack entry (nth previous working directory) is removed. With no argument, the pushd function switches the top two entries (current and previous working directory) and changes directory to the previous working directory. This is equivalent to "cd ?/span>". If a directory argument is given, pushd puts the directory on the top of the directory stack and changes directory to it. If a +n argument is given, pushd puts the nth directory stack entry (nth previous working directory) on the top of the stack and changes directory to it. # # Sample C Shell Directory Management Functions # # Function fcd - verify accessibility before changing # to target directory function fcd { # Make sure directory exists if [[ ! -d $1 ]] then print "$1: No such file or directory" return 1 else # Make sure directory is searchable if [[ ! -x $1 ]] then print "$1: Permission denied" return 1 fi fi # Otherwise change directory to it cd $1 return 0 } # Function dirs - display directory stack function dirs { # Display current directory and directory stack print "$PWD ${DSTACK[*]}" } # Function pushd - add entry to directory stack function pushd { # Set stack depth (number of stack entries) integer DEPTH=${#DSTACK[*]} case $1 in "" )# No argument - switch top 2 stack elements if ((${#DSTACK[*]} < 1)) then print "$0: Only one stack entry." return 1 else fcd ${DSTACK[0]} || return DSTACK[0]=$OLDPWD dirs fi ;; +@([1-9])*([0-9])) # Number argument 1-999* - move entry to top # of directory stack and cd to it integer ENTRY=${1#+} if ((${#DSTACK[*]} < $ENTRY)) then print "$0: Directory stack not that deep" return 1 else fcd ${DSTACK[ENTRY-1]} || return DSTACK[ENTRY-1]=$OLDPWD dirs fi ;; * ) # Directory argument - verify argument # before changing directory and adjusting # rest of directory stack fcd $1 || return until ((DEPTH == 0)) do DSTACK[DEPTH]=${DSTACK[DEPTH-1]} ((DEPTH-=1)) done DSTACK[DEPTH]=$OLDPWD dirs ;; esac } # Function popd - remove entry from directory stack function popd { # Set stack depth (number of stack entries) integer i=0 DEPTH=${#DSTACK[*]} ENTRY=${1#+} case $1 in "" ) # No argument - discard top stack entry if ((${#DSTACK[*]} < 1)) then print "$0: Directory stack empty." return 1 else fcd ${DSTACK[0]} || return while ((i < (DEPTH-1))) do DSTACK[i]=${DSTACK[i+1]} ((i+=1)) done unset DSTACK[i] dirs fi ;; +@([1-9])*([0-9])) # Number argument 1-999* - discard nth # stack entry if ((${#DSTACK[*]} < ENTRY)) then print "$0: Directory stack not that deep" return 1 else while ((ENTRY < DEPTH)) do DSTACK[ENTRY-1]=${DSTACK[ENTRY]} ((ENTRY+=1)) done unset DSTACK[ENTRY-1] dirs fi ;; # Invalid argument given * ) print "$0: Invalid argument." return 1 ;; esac } |