[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: cd *
From: |
Chet Ramey |
Subject: |
Re: cd * |
Date: |
Mon, 26 Feb 2001 09:09:28 -0500 |
> > Observations: 1. bash doesn't feel bad about more than one argument to
> > cd. 2. --help should be implemented. 3. -P, -L should be explained,
> > lest one need to wait for the whole bash manual to pop up. Wait,
> > before any, um, excuses are made, shouldn't any of this behaviour
> > produce even one warning message about multiple arguments?:
>
> If bash is striving to include ksh behavior, it will need to be able to
> accept cd with two directories:
It's not.
> cd /usr/local/lib
> cd lib bin
> pwd
>
> will produce "/usr/local/bin" w/ ksh.
>
> But cd with more than two non-option arguments should be an error.
This is trivial to do with a shell function. Here's one that implements
a ksh-like cd.
#
# ksh-like `cd': cd [-LP] [dir [change]]
#
cd()
{
OPTIND=1
while getopts "LP" opt
do
case $opt in
L|P) CDOPTS="$CDOPTS -$opt" ;;
*) echo "$FUNCNAME: usage: $FUNCNAME [-LP] [dir] [change]"
>&2
return 2;;
esac
done
shift $(( $OPTIND - 1 ))
case $# in
0) builtin cd $CDOPTS "$HOME" ;;
1) builtin cd $CDOPTS "$@" ;;
2) old="$1" new="$2"
case "$PWD" in
*$old*) ;;
*) echo "${0##*/}: $FUNCNAME: bad substitution" >&2 ;
return 1 ;;
esac
dir=${PWD//$old/$new}
builtin cd $CDOPTS "$dir" && echo "$PWD"
;;
*) echo "${0##*/}: $FUNCNAME: usage: $FUNCNAME [-LP] [dir]
[change]" >&2
return 2 ;;
esac
}
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
( ``Discere est Dolere'' -- chet)
Chet Ramey, CWRU chet@po.CWRU.Edu http://cnswww.cns.cwru.edu/~chet/
- cd *, Dan Jacobson, 2001/02/23
- Re: cd *, Chet Ramey, 2001/02/26
- Re: cd *,
Chet Ramey <=