zsh-users
 help / color / mirror / code / Atom feed
* Testing for broken symlinks
@ 2005-04-20  7:30 Eric Mangold
  2005-04-20  9:15 ` Peter Stephenson
  2005-04-20 10:51 ` Alexey Tourbin
  0 siblings, 2 replies; 5+ messages in thread
From: Eric Mangold @ 2005-04-20  7:30 UTC (permalink / raw)
  To: zsh-users

Hello,

Is there any way to test if a symlink is broken?

Trying to read from the file and catching the errors would suffice for me,  
but I'm curious if there is a more elegant method.

-Eric


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Testing for broken symlinks
  2005-04-20  7:30 Testing for broken symlinks Eric Mangold
@ 2005-04-20  9:15 ` Peter Stephenson
  2005-04-20 17:20   ` Paul Lew
  2005-04-20 10:51 ` Alexey Tourbin
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2005-04-20  9:15 UTC (permalink / raw)
  To: zsh-users

"Eric Mangold" wrote:
> Is there any way to test if a symlink is broken?

This will work:

  broken=(mylinktest(N-@))

  if (( ${#broken} )); then
    print mylinktest is a broken symbolic link
  else
    print mylinktest is not a symbolic link or is not broken
  fi

The basic point is that the command line argument mylinktest(N-@) is
removed unless mylinktest is a broken symbolic link, so you may have a
more efficient way to use the fact.

This works because - makes the shell follow symbolic links, while @
tells it to look only for symbolic links.  The only files that are
still resolved as symbolic links are those that are broken.  The N flag
then removes any failed glob, i.e. those not broken symbolic links.

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


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

**********************************************************************


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Testing for broken symlinks
  2005-04-20  7:30 Testing for broken symlinks Eric Mangold
  2005-04-20  9:15 ` Peter Stephenson
@ 2005-04-20 10:51 ` Alexey Tourbin
  1 sibling, 0 replies; 5+ messages in thread
From: Alexey Tourbin @ 2005-04-20 10:51 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 331 bytes --]

On Wed, Apr 20, 2005 at 05:30:51PM +1000, Eric Mangold wrote:
> Is there any way to test if a symlink is broken?

if [ -L "$f" -a ! -e "$f" ]; then
	print "$f: broken symlink"
fi

$ ln -sf /asdf/zxcv .
$ test -L zxcv; echo $?
0
$ test -e zxcv; echo $?
1
$

The point is that lstat(2) succeeds but stat(2) fails on broken
symlinks.

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Testing for broken symlinks
  2005-04-20  9:15 ` Peter Stephenson
@ 2005-04-20 17:20   ` Paul Lew
  2005-04-21  7:02     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Paul Lew @ 2005-04-20 17:20 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

>>>>> "Peter" == Peter Stephenson <pws@csr.com> writes:

    Eric>> Is there any way to test if a symlink is broken?

    Peter> This will work:

    Peter> broken=(mylinktest(N-@))

    Peter> if (( ${#broken} )); then
    Peter> print mylinktest is a broken symbolic link
    Peter> else
    Peter> print mylinktest is not a symbolic link or is not broken
    Peter> fi
    Peter> <snip>

A off-topic question, what ever happen to the old feature (some
eariler version of zsh) that displays an '&' at end of the bad symlink
when displaying filename completion lists?  e.g.,

   > touch foo1 foo2
   > ln -s not-exist foo3
   > ls foo<tab>
   foo1@   foo2@   foo3&


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Testing for broken symlinks
  2005-04-20 17:20   ` Paul Lew
@ 2005-04-21  7:02     ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2005-04-21  7:02 UTC (permalink / raw)
  To: Paul Lew; +Cc: zsh-users

On Apr 20, 10:20am, Paul Lew wrote:
}
} A off-topic question, what ever happen to the old feature (some
} eariler version of zsh) that displays an '&' at end of the bad symlink
} when displaying filename completion lists?

That must have been a *really* long time ago.  I find some commented-out
code in zsh-2.4/src/glob.c that lingers as far as 3.0.8's Src/glob.c,
but it's gone in 3.1.6 and later.  I cannot recall ever using a build of
zsh where that code was live.


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-04-21  7:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-04-20  7:30 Testing for broken symlinks Eric Mangold
2005-04-20  9:15 ` Peter Stephenson
2005-04-20 17:20   ` Paul Lew
2005-04-21  7:02     ` Bart Schaefer
2005-04-20 10:51 ` Alexey Tourbin

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).