[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] No tilde expansion in [[..]] when reading input with rea

From: Greg Wooledge
Subject: Re: [Help-bash] No tilde expansion in [[..]] when reading input with read
Date: Mon, 25 Feb 2013 08:37:03 -0500
User-agent: Mutt/

On Mon, Feb 25, 2013 at 09:38:59AM +0800, Chris Down wrote:
> I'd be inclined to say `getent' would probably make it less portable than
> hardcoding /etc/passwd.

In the general case, user accounts may not be defined in passwd at all.
They may come from NIS, or NIS+, or LDAP, or whatever other strange
and exotic beasts roam the wilds of authentication.

Unfortunately, getent is not portable either.

Ironically, stripping all characters except alphanumeric, underscore,
and maybe a few other well-chosen bits of punctuation, and then using
eval, may be the safest and most portable way to go.

IFS= read -r raw_username
if [[ $username = "$raw_username" ]]; then
  eval "home=~$username"
  echo "home=<$home>"
  echo "Nice try, but no."

imadev:~$ ./foo

imadev:~$ ./foo
john;rm -rf ~
Nice try, but no.

And for the record, my account is defined in NIS with a shell override in

imadev:~$ grep wooledg /etc/passwd

So parsing /etc/passwd would not have worked, and this OS does not have
getent either.

reply via email to

[Prev in Thread] Current Thread [Next in Thread]