Re: bash (or other shell) help with usernames
Todd A. Jacobs <
nospam@...>
2011-07-20 23:27:36 GMT
On Jul 19, 3:35 pm, Roger <roger.in.eug...@...> wrote:
> #!/bin/bash
> touch ~$1/FILENAME
This won't work because of the way that bash handles the quoting. You
could use eval:
eval "touch ~${1}/FILENAME"
so that the positional parameter is expanded and then re-processed as
a tilde expansion, but that's rather unsafe. You're better off hard-
coding the path to the home directory like so:
touch /home/${1}/FILENAME
which should be fine unless you have multiple homes for different
users (e.g. if you're using NFS, or have thousands of users and are
splitting home partitions onto different spindles for performance
reasons).
There are also other ways to get the home directory for a user that
don't require tilde expansion. For example, given a username of foo:
set -- foo
getent passwd $1 | cut -d: -f6
You could use that to build the path, which might be useful in the
event that you can't assume that the user's home directory will be
under /home.
(Continue reading)