[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How to extrapolate bash string?
From: |
Eric Blake |
Subject: |
Re: [Help-bash] How to extrapolate bash string? |
Date: |
Thu, 12 Sep 2013 13:05:59 -0600 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130805 Thunderbird/17.0.8 |
On 09/11/2013 10:22 PM, Chris Down wrote:
> On 2013-09-11 22:30, Peng Yu wrote:
>> This message does not seem to provide a solution. If 'eval' is not
>> used, what else can be used?
>
> It's a thread, I'm not referring to that individual message.
In particular,
https://lists.gnu.org/archive/html/bug-bash/2013-04/msg00051.html mentions:
case $1 in
\~ )
printf '%s\n' "$HOME" ;;
\~/* )
printf '%s\n' "$HOME/${1#??}" ;;
\~* )
user=${1%%/*}
path=${1#*/}
# Sanitize user before feeding it to eval.
# You must adjust this code based on what characters are legal in your
# system's usernames. If your system allows shell metacharacters in
# usernames, you are screwed. Just give up now (switch to perl).
user=${user#\~}
user2=${user//[^[:alnum:]._-]/}
if [[ $user != "$user2" ]]; then
echo "Error: invalid characters in username" >&2
exit 1
fi
eval "home=~$user2"
case $1 in
*/* ) printf '%s\n' "$home/$path" ;;
* ) printf '%s\n' "$home" ;;
esac ;;
* )
printf '%s\n' "$1" ;;
esac
and followups pointed out that instead of erroring out for shell
metacharacters, that it should just print them as-is (after all, the
only way to combine ~ and a metacharacter into a single word is to use
quoting, but quoting disables tilde-expansion).
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
signature.asc
Description: OpenPGP digital signature