Subject: Re: bug#10342: Bug Report for command "dirname"

From: Eric Blake <[email-masked]>
Date: Wed, 21 Dec 2011 11:22:28 -0700

On 12/21/2011 11:06 AM, Eric Blake wrote:
>> But when i tried using ----->   dirname xyz
>> it gave output as <.>   { output states , present directory
> 
> http://pubs.opengroup.org/onlinepubs/9699919799/utilities/dirname.html
> 
> I'm closing this bug report, as there is no bug here.  However, feel
> free to make further comments if you need more clarification, or help in
> determining if 'dirname' really is the best fit for the shell scripting
> task you were really attempting to solve.

>> If it had stated no such directory ,it would have served my purpose .

Preemptively, I'm going to guess what you were trying to do (but of
course, stating your actual goal will get you further if my guess is on
the wrong track) - given an arbitrary string in $str, determine whether
that string represents an existing file; if so, report the absolute path
of the directory that contains the file, and if not, report that no such
file exists.

In which case, this will do the trick (using GNU readlink):

readlink -ve -- "$str" || dirname -- "$(readlink -e -- "$str")"

It is also possible to do this without forking readlink or dirname, but
instead doing it entirely in POSIX shell using constructs like ${str%/*}
and test -f.  Making it portable to Solaris /bin/sh is tougher.

-- 
Eric Blake   [email-masked]    +1-919-301-3266
Libvirt virtualization library http://libvirt.org