From: George Ogata Date: 2007-01-30T19:22:48+09:00 Subject: Re: How to get the unix login name of the calling user? On 1/30/07, 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? * 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.org/stdlib/ (Although I've just noticed that clicking on 'etc' gives a 500...)