801a4909a83114a021fc441c496ec0b83600d7e2
[platform/upstream/bash.git] / examples / functions / ksh-cd
1 #
2 # ksh-like `cd': cd [-LP] [dir [change]]
3 #
4 cd()
5 {
6         OPTIND=1
7         while getopts "LP" opt
8         do
9                 case $opt in
10                 L|P)    CDOPTS="$CDOPTS -$opt" ;;
11                 *)      echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
12                         return 2;;
13                 esac
14         done
15
16         shift $(( $OPTIND - 1 ))
17
18         case $# in
19         0)      builtin cd $CDOPTS "$HOME" ;;
20         1)      builtin cd $CDOPTS "$@" ;;
21         2)      old="$1" new="$2"
22                 case "$PWD" in
23                 *$old*) ;;
24                 *)       echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ; return 1 ;;
25                 esac
26
27                 dir=${PWD//$old/$new}
28
29                 builtin cd $CDOPTS "$dir" && echo "$PWD"
30
31                 ;;
32         *)      echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]" >&2
33                 return 2 ;;
34         esac
35 }