Roger | 19 Jul 21:35
Picon

bash (or other shell) help with usernames

Trying to do something simple, but it's not working.
Just trying to pass an argument (in this case a username), and have it
touch a file in that user's directory.
calling the script like this:
SCRIPTNAME  USERNAME

#!/bin/bash
touch ~$1/FILENAME

Does not work.  I get no such directory or file.  If I hard code in:
touch  ~USERNAME/FILENAME

Then it works.
If I call it with:
SCRIPTNAME   ~USERNAME    (Note the ~)

There must be something going on with passing in the username via the
command prompt and using that string with tilde expansion.

I'm trying to do something when a user connects to an SMB share on
linux.  I can retrieve the username via $U in the pre-exec area.  I
could add the full path in SMB.conf, but not all users are in the same
path for their home directories.   Using the tilde with their username
is best.

--

-- 
Roger

Todd A. Jacobs | 21 Jul 01:27
Favicon

Re: bash (or other shell) help with usernames

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)

Roger | 21 Jul 17:03
Picon

Re: Re: bash (or other shell) help with usernames

> 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.

I think this will be it.  there are 2 different path possibilities,
one for students, the other for staff.  'touch' was an example, I'm
actually going to be setting permissions on a sub-directory in their
home directory when they connect via SMB or AFP.

--

-- 
Roger

Stephane CHAZELAS | 27 Sep 22:02
Picon
Favicon

Re: bash (or other shell) help with usernames

2011-07-19, 12:35(-07), Roger:
> Trying to do something simple, but it's not working.
> Just trying to pass an argument (in this case a username), and have it
> touch a file in that user's directory.
> calling the script like this:
> SCRIPTNAME  USERNAME
>
> #!/bin/bash
> touch ~$1/FILENAME
[...]

That's zsh syntax, not bash (though there are limits on the
format of username accepted, for instance a "a+b" user won't be
recognised)

You'd be better of using perl or another high level programming
language even if using touch to touch the file/dir.

#! /usr/bin/perl -w
foreach my $user (@ARGV) {
  my $home = (getpwnam($user))[7] or
    die "No such user $user or no home directory";
  system("touch", "--", "$home/FILENAME") == 0 or exit(1);
}

--

-- 
Stephane


Gmane