Eric Blake <ebb9@xxxxxx> wrote, on 15 Jul 2009:
>
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> According to Geoff Clare on 7/15/2009 8:00 AM:
> > Both techniques can be used with ~ just the same as with "$HOME",
> > but you have a point that the more modern "${dir%/}" technique
> > can be used with "$HOME" but not with ~.
>
> >
> > case $dir in
> > */) pathname=$dir$filename ;;
> > *) pathname=$dir/$filename ;;
> > esac
>
> In this case, a direct translation to tilde-expansion won't work:
>
> */) pathname=~$filename ;;
>
> because it forms a tilde-prefix with characters not valid in LOGNAME
> (~$filename) and thus gives unspecified behavior.
Yes, I should have been more careful when I said "just the same".
> Using a temporary variable could be an option:
>
> tmp=~root
> case $tmp in
> */) pathname=$tmp$filename ;;
> *) pathname=~root/$filename ;;
> esac
Yes, that's roughly what I had in mind. (I would have used
$tmp/$filename instead of ~root/$filename.)
> but it would certainly be nicer to guarantee that ~root/$filename always
> specified the desired file, without needing an intermediate variable.
>
> >
> > or:
> >
> > pathname=$dir//$filename
>
> This approach works to avoid the implementation-defined nature of //, but
> doesn't work if $dir is exactly //.
I think this method is considered safe when forming a pathname for
a (real) file because in that case $dir necessarily is a "normal"
directory (one that contains real files) and so could not be //
on systems where the component after // is a server name.
--
Geoff Clare <g.clare@xxxxxx>
The Open Group, Thames Tower, Station Road, Reading, RG1 1LX, England
|