[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to get the unix login name of the calling user?

Moritz Reiter

1/30/2007 9:57:00 AM

Hi,

I am writing a script which uses a configuration file. I already managed
that it is possible for the caller to specify the location of the config
file as a command line parameter (which was pretty easy with optparse).
Now I want to set the default location of for the config file to
/home/username/.my_config_file. Therefore I have to get the username of
the user who is calling the script.

My idea would be to parse /etc/passwd for the return value of
Process.uid but as I am pretty unexperienced in ruby I wanted to ask
here if someone could tell me a more elegant method to do it?

Thanks,
mo

--
Posted via http://www.ruby-....

8 Answers

George

1/30/2007 10:23:00 AM

0

On 1/30/07, Moritz Reiter <mo@agrav.org> wrote:
> Hi,
>
> I am writing a script which uses a configuration file. I already managed
> that it is possible for the caller to specify the location of the config
> file as a command line parameter (which was pretty easy with optparse).
> Now I want to set the default location of for the config file to
> /home/username/.my_config_file. Therefore I have to get the username of
> the user who is calling the script.
>
> My idea would be to parse /etc/passwd for the return value of
> Process.uid but as I am pretty unexperienced in ruby I wanted to ask
> here if someone could tell me a more elegant method to do it?

* If you just need the user's home directory, use
File.expand_path('~/.my_config_file').

* If you actually need the user's login name, the USER envvar is
usually (always?) set to this -- so ENV['USER'].

* If you really need to traipse through passwd ruby also comes with an
'etc' file parsing library.

require 'etc'

Then:

user = Etc.getpwuid(Process.uid).name

Or:

effective_user = Etc.getpwuid(Process.euid).name

More info:

http://ruby-doc.o...
(Although I've just noticed that clicking on 'etc' gives a 500...)

Moritz Reiter

1/30/2007 10:35:00 AM

0

George Ogata wrote:
> * If you just need the user's home directory, use
> File.expand_path('~/.my_config_file').

Great! I think this first suggestion will do it in this case, but the
other information is very valuable, too. Thank you! :)

--
Posted via http://www.ruby-....

Thomas Hafner

1/30/2007 10:42:00 AM

0

Moritz Reiter <mo@agrav.org> wrote/schrieb <d1d81a8b82b36ce7f4843e7949c729c0@ruby-forum.com>:

> Now I want to set the default location of for the config file to
> /home/username/.my_config_file. Therefore I have to get the username
> of the user who is calling the script.

Not necessarily. How about that?:
File.expand_path("~/.my_config_file")
=> /home/username/.my_config_file

Regards
Thomas

Moritz Reiter

1/30/2007 11:01:00 AM

0

Thomas Hafner wrote:
> Not necessarily. How about that?:
> File.expand_path("~/.my_config_file")
> => /home/username/.my_config_file

Yo, that works perfectly, thanks!

--
Posted via http://www.ruby-....

Stephane Wirtel

1/30/2007 12:19:00 PM

0

> File.expand_path('~/.my_config_file').
Why not ENV['HOME'] ?

Moritz Reiter

1/30/2007 1:18:00 PM

0

Stephane Wirtel wrote:
>> File.expand_path('~/.my_config_file').
> Why not ENV['HOME'] ?

Yes, I think in my case both solutions are equivalent.

--
Posted via http://www.ruby-....

Ken Bloom

1/30/2007 3:06:00 PM

0

On Tue, 30 Jan 2007 18:57:08 +0900, Moritz Reiter wrote:

> Hi,
>
> I am writing a script which uses a configuration file. I already managed
> that it is possible for the caller to specify the location of the config
> file as a command line parameter (which was pretty easy with optparse).
> Now I want to set the default location of for the config file to
> /home/username/.my_config_file. Therefore I have to get the username of
> the user who is calling the script.
>
> My idea would be to parse /etc/passwd for the return value of
> Process.uid but as I am pretty unexperienced in ruby I wanted to ask
> here if someone could tell me a more elegant method to do it?
>
> Thanks,
> mo
>

Just to add to what everyone else has said so far about using
File.expand_path('~/.my_config_file') or ENV['HOME'], there is no
requirement on UNIX that the home directories be located in
/home/username. They could be placed in other trees (for example on an NFS
share somewhere) or they may be buried several levels deep -- a common
practice in large computer labs is to split them into directories based on
their first couple letters (for easier directory access) for example
/home/k/ka/kabloom.

Thus, trying to access /home/#{username}/.my_config_file is precisely the
wrong thing to do. Instead, use File.expand_path('~/.my_config_file') or
#{ENV['HOME']}/.my_config_file

--Ken


--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Gary Wright

1/30/2007 4:04:00 PM

0


On Jan 30, 2007, at 10:10 AM, Ken Bloom wrote:
> Thus, trying to access /home/#{username}/.my_config_file is
> precisely the
> wrong thing to do. Instead, use File.expand_path
> ('~/.my_config_file') or
> #{ENV['HOME']}/.my_config_file

These methods all depend on environment variables. If you need/want to
find this information based on the actual userid and home directory
information maintained by the OS:

require 'etc'

homedir = Etc.getpwuid(Process.uid).dir

Gary Wright