LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-24-2007, 01:32 PM   #1
Penguin of Wonder
Senior Member
 
Registered: Sep 2005
Location: West Virginia
Distribution: Gentoo
Posts: 1,249

Rep: Reputation: 45
Check root access in Ruby


I'm using Ruby to write a fairly simple script but the script needs root access to run properly. Is there a simple method to check if the user is root in Ruby. I know how to in Bash and I've found easy enough examples for Perl, but a method Ruby evades me.

Any suggestions?
 
Old 07-25-2007, 07:03 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
You can see the environment with the following Ruby snippet:
Code:
ENV.each do |key,value| puts "#{key} = #{value}"; end
You'll see there are entries for "USER" and "USERNAME" -- the former is the one you'll want to check for the sake of confirming permissions. If the script is run through sudo, "USER" will be the sudo-ed-to user (usually root) and "USERNAME" will be the user who ran sudo. (Of course, sudo sets other environment variables, but I presume that "USERNAME" would reveal the original UID of other programs that change permissions during the course of execution as well.)

Oh, also, ENV is a hash, in case that wasn't apparent. ENV["USER"] gets the value.
 
Old 07-25-2007, 09:04 PM   #3
Penguin of Wonder
Senior Member
 
Registered: Sep 2005
Location: West Virginia
Distribution: Gentoo
Posts: 1,249

Original Poster
Rep: Reputation: 45
Thank you very much! I'm in the process of learning ruby so I have some reading to do to fully understand it. But you have at least set me on the right track and thats what I really needed. Again, thank you.
 
  


Reply

Tags
ruby



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP v Ruby/Ruby on Rails Neko_D Linux - General 4 02-08-2007 11:56 PM
[Ruby] Tar'ing or Zip'ing with ruby script lmcilwain Programming 2 01-19-2007 06:56 PM
How to check if i'm root in a script? Maverick1182 Linux - Newbie 4 10-07-2006 05:20 PM
Granting SSH access to a Ruby on Rails user calande Linux - Security 1 01-12-2006 02:33 PM
Forced Root Filesystem Check Mr_Penguin_02 Linux - Newbie 2 08-15-2004 07:37 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:20 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration