[nzlug] Checking for directories that are symbolic links

Volker Kuhlmann hidden at paradise.net.nz
Fri Feb 23 19:50:57 NZDT 2007


On Fri 23 Feb 2007 17:11:03 NZDT +1300, Johann Schoonees wrote:

> In bash scripts, how would I check if a directory name is a symlink or 
> not?  The conditional in
> 
> if [ -d $dirname ]
> 
> evaluates to true whether dirname is a symlink or not.

man test

  if [ -d "$dirname" -a -L "$dirname" ]

Btw your script will fail sooner or later without doublequotes around
variables which contain filenames. Essentially, use quotes for all
variables holding string values unless you can guarantee they only
contain letters and digits (and a few other characters). It's not only a
question of a script not doing what it should, it's a safety issue too.
There's a huge number of shoddy shellscripts around.

Volker

-- 
Volker Kuhlmann			is list0570 with the domain in header
http://volker.dnsalias.net/	Please do not CC list postings to me.




More information about the NZLUG mailing list