Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Full path with ksh emulation



"Tony Hasler" <tony@xxxxxxxxxxxxxxxxxxxx> wrote:
> Accordingly, they have hit upon the idea of using zsh to emulate ksh.  That
> certainly solves the original problem, but introduces a new one.  In ksh the
> 'whence' command always gives you the absolute path of its argument.  So
> 'whence $0' always gives a full path even if the command was executed by
> typing './myscript'.  I can find no straightforward way to do this in zsh.

Put the following function after the "emulate ksh".  It doesn't
cover all possibilities but it should do the basics.  (It's annoying
there's apparently no way of rationalising the path to a directory without
changing into it.)

whence () {
  # Version of whence which expands the full path to an
  # executable.  Uses the builtin whence if the argument
  * is not found in the path.
  # N.B.: doesn't test if the argument matches an alias, builtin
  # or function first, unlike the builtin.
  local p f
  # Ensure pushd doesn't ignore duplicates,
  # and doesn't output messages
  emulate -L zsh
  setopt pushdsilent

  if [[ $1 != /* ]]; then
    for p in $path; do
      if [[ -x $p/$1 ]]; then
        f=$p/$1
	# Temporarilly switch to the directory of the file found.
	# This rationalises the directory path.
        pushd $f:h
        print $PWD/$f:t
        popd
        return
      fi
    done
  fi
  builtin whence $1
}

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


Your mail client is unable to display the latest news from CSR. To access our news copy this link into a web browser:  http://www.csr.com/email_sig.html



Messages sorted by: Reverse Date, Date, Thread, Author