Testing if a script is run via sudo

Is tehre a way to test if a ruby script is run as a sudoer user ?

For the time being i’m using a zsh script to test that however I’d like
using ruby only.

the zsh i’m using :
#! /usr/bin/env zsh

[ “$SUDO_USER” -a id -u -eq 0 ] || {
echo “false”
exit 0
}
echo “true”
exit 0

Une Bévue wrote:

Is tehre a way to test if a ruby script is run as a sudoer user ?

on my system, echo $USER gives root under sudo (mac os x).

So in ruby:

ENV[‘USER’]

will return root under sudo user, and my name normally.

Note that here i have done a “sudo bash” first.

Une Bévue wrote:

Is tehre a way to test if a ruby script is run as a sudoer user ?

$ sudo ruby -e “require ‘etc’; puts Process.uid, Process.euid,
Etc.getlogin”
0
0
brian

… so I’m running as root, but I originally logged in as brian.

2010/10/7 Une Bévue [email protected]

Is tehre a way to test if a ruby script is run as a sudoer user ?

A cleaner way would be to use the Process::UID module, namely the #eid
method.

HTH
Ammar

Brian C. [email protected] wrote:

$ sudo ruby -e “require ‘etc’; puts Process.uid, Process.euid,
Etc.getlogin”
0
0
brian

… so I’m running as root, but I originally logged in as brian.

i see, nice !

Rahul K. [email protected] wrote:

on my system, echo $USER gives root under sudo (mac os x).

So in ruby:

ENV[‘USER’]

will return root under sudo user, and my name normally.

Note that here i have done a “sudo bash” first.

I’m using too Mac OS X (zsh as default shell) i don’t get samething as u
:

imyt% sudo echo $USER
Password:
yt
imyt% echo $USER
yt
imyt%

however “yt” -is- a sudoer…

Ammar A. [email protected] wrote:

A cleaner way would be to use the Process::UID module, namely the #eid method.

fine, thanks !