AtomicPorkchop
AtomicPorkchop

Reputation: 2675

How can I check for root when a user runs a perl script

I am working a script to generate a config file that I want to store in /etc/solignis. When I run the script as a limited user it does not allow me to create the directory or write the file. So the script will have to run as sudo or a root user, how can I check if the user is a root or atleast using sudo?

Upvotes: 15

Views: 14475

Answers (4)

jtylers
jtylers

Reputation: 11

I had the same question and ended up using the results of all three previous answers:

die "Must run as root\n" if $> != 0;

Upvotes: 1

user2337958
user2337958

Reputation: 1

if ( $< != 0 ) {
print "This script must be run as root\n"; 
exit (0);
}

Upvotes: 0

Sonia Hamilton
Sonia Hamilton

Reputation: 4419

XAppSoftware: How to check for root user has a solution:

my $login = (getpwuid $>);
die "must run as root" if $login ne 'root';

Upvotes: 5

cjm
cjm

Reputation: 62109

If $> (aka $EFFECTIVE_USER_ID if you use English) is non-zero, then the user is not root.

Upvotes: 31

Related Questions