help-bash
[Top][All Lists]
Advanced

[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/1.4.2.3i

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
username=${raw_username//[![:alnum:]_.-]/}
if [[ $username = "$raw_username" ]]; then
  eval "home=~$username"
  echo "home=<$home>"
else
  echo "Nice try, but no."
fi

imadev:~$ ./foo
wooledg
home=</net/home/wooledg>

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

And for the record, my account is defined in NIS with a shell override in
/etc/passwd:

imadev:~$ grep wooledg /etc/passwd
+wooledg::-24:-24:::/usr/local/bin/bash

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]