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

FW: Full path with ksh emulation



Peter,

Thanks for the idea of using pushd (cd) to rationalise the path.  One of
those ideas that seems blindingly obvious....after someone explains it to
you.

Never having learnt zsh I tried your routine and there was bad news and good
news.  

The bad news is that it didn't work.  It tries to find the argument in
$PATH, but when "." isn't there it reverts to the builtin.  When "." is in
the path you get an absolute path, but one with a "." in it.  Only when the
absolute path of "." is in $PATH and is the first place with an executable
copy of the script does it seem to work.

The good news (I think) is that I learn't enough of what you were trying to
do to fix it (again I think).

Do you see a flaw in the following version?

Incidentally, can one put this function - and the call to emulate ksh - in
the .zshrc file?

Once again, thanks for your time.

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 [[ $(builtin whence $1) == .* ]]; then
	# Temporarilly switch to the directory of the file found.
	# This rationalises the directory path.
        pushd $1:h
	  p=$PWD/$1:t
	  print $PWD/$1:t
	  popd
	  return
  fi
  builtin whence $1
}


--Tony
-----Original Message-----
From: Peter Stephenson [mailto:pws@xxxxxxx] 
Sent: 05 January 2006 12:45
To: zsh-users@xxxxxxxxxx
Subject: 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