Package: moreutils
Version: 0.31
Severity: wishlist
Tags: patch

I have often wanted a command combining mkdir and cd. I looked around and 
found several blog and forum posts showing quick and dirty implementations, 
but none that were complete enough to include in a distribution, so I wrote 
this one.

Do you think this would fit in the Moreutils collection? It is admittedly 
somewhat different from most of the other tools in that it can't be used in 
a pipeline.

I was originally going to call it "mkcd", but that's apparently a CD making 
program in Solaris and Mandriva. "mcd" is taken by Mtools, to I decided on 
"mkcdir".

The command is implemented as a shell function, as a child process can't 
change the working directory of its parent. I don't expect it to work in a 
plain Bourne shell, but I have tested it in Bash, Kornshell and the Z Shell. 
I don't have an implementation for the C Shell. That would have to be quite 
different.


-- System Information:
Debian Release: 5.0.9
  APT prefers oldstable
  APT policy: (500, 'oldstable')
Architecture: i386 (i586)

Kernel: Linux 2.6.26-2-486
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages moreutils depends on:
ii  libc6                    2.7-18lenny7    GNU C Library: Shared libraries
ii  perl                     5.10.0-19lenny5 Larry Wall's Practical Extraction 

moreutils recommends no packages.

Versions of packages moreutils suggests:
pn  libtime-duration-perl         <none>     (no description available)
ii  libtimedate-perl              1.1600-9   Time and date functions for Perl

-- no debconf information
function mkcdir {
   # Declare local variables.
   typeset parameter=
   typeset name=
   typeset name_count=0
   typeset print_version=
   typeset print_help=

   # Analyze the command line.
   for parameter in "$@" ; do
      case "${parameter}" in
         --version)
            print_version=on
            ;;
         --help)
            print_help=on
            ;;
         -*)
            # It's an option for mkdir.
            ;;
         *)
            # It's a directory name.
            name="${parameter}"
            name_count=$(($name_count + 1))
            ;;
      esac
   done

   # Print the version and/or help if requested. (This is implemented mostly to
   # catch those parameters and avoid passing them to mkdir.)
   if [ "${print_version}" ] ; then
      echo 'mkcdir 1'
      echo
   fi
   if [ "${print_help}" ] ; then
      echo 'mkcdir combines mkdir and cd; it makes a directory and goes into 
it.'
      echo 'Any options are passed along to mkdir.'
      echo 'Only the directory name is passed to cd.'
      echo
   fi
   if [ "${print_version}" -o "${print_help}" ] ; then
      return 0
   fi

   # Check that exactly one directory name was passed.
   if [ $name_count -ne 1 ] ; then
      echo 'mkcdir requires one directory name.' >&2
      return 2
   fi

   # Do the work.
   mkdir "$@" && cd "${name}"
}

Reply via email to