LinuxQuestions.org
Help answer threads with 0 replies.
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 01-06-2010, 01:11 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Variable doesn't hold proper value


I was creating a script and I can't figure out why this isn't working....
Code:
my $priv = system "whoami";
if ($priv ne "root"){
	say "FATAL ERROR: you may only run this as root.  User found running script: $priv";
	die;
}
when I try to execute it, this happens:
Code:
root
FATAL ERROR: you may only run this as root.  User found running script: 0
Died at hostcheck.pl line 42.
Why is my value changed to 0? It's like it's checking it as a scalar value...
 
Old 01-06-2010, 01:20 PM   #2
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
The system function simply runs the code. What's returned is the return value of the utility. In this case if you print $priv it will be a 0 for success. Try using popen or another pipe function that lets you treat your subprocess as a file.

Regards,

Alunduil
 
Old 01-06-2010, 01:39 PM   #3
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Why not just use the function getlogin()?

Code:
#!/usr/bin/perl

print "Your username is " .getlogin();
 
Old 01-06-2010, 03:42 PM   #4
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
I am new to perl, had no idea that function existed. I'll sue that, thanks!

edit:

getlogin(); wont work for me. It's tied to my login session somehow. I would like to be able to sudo this script, or use su to switch my user from a terminal. It always returns my regular login

Last edited by hawk__0; 01-06-2010 at 03:45 PM.
 
Old 01-06-2010, 04:04 PM   #5
devnull10
Member
 
Registered: Jan 2010
Location: Lancashire
Distribution: Slackware Stable
Posts: 572

Rep: Reputation: 120Reputation: 120
Well, you know root will be userid 0, so you could use $<:


Code:
#!/usr/bin/perl

print "User id of current user is $< \n"
This works with su however I haven't tried it with sudo - I can't see why it wouldn't.
 
Old 01-06-2010, 04:23 PM   #6
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
that works, thanks!
 
Old 01-07-2010, 01:55 AM   #7
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
That's great it worked.

For your issue
Code:
 
my $priv = system "whoami";
if ($priv ne "root"){
	say "FATAL ERROR: you may only run this as root.  User found running script: $priv";
	die;
}
This will work as well, you need to collect the output of "whoami" in the variable.

Code:
my $priv = `whoami`;
chomp($priv);
if ($priv ne "root"){
	say "FATAL ERROR: you may only run this as root.  User found running script: $priv";
	die;
}

Mind the backticks in the whoami command they are not single quote.
 
  


Reply



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
Script to copy specific directory based on variable to folder with that variable name fluxburn Programming 7 01-07-2010 07:59 PM
Command not found and a proper path variable thelonesquirrely Linux - General 7 03-18-2009 06:28 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 12:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM
function showing a list of variable and value: (dynamic variable name) budhax Linux - Newbie 1 09-19-2008 07:05 PM

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

All times are GMT -5. The time now is 06:48 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