Operating System - HP-UX
1830973 Members
2401 Online
110018 Solutions
New Discussion

Re: call system command and store result in variable

 
Mike Keys
Regular Advisor

call system command and store result in variable

Need help. Trying to run the output of the following command and store in a variable. Can't get to work.

print "Enter ID of user to modify: "; $mod = ;
$REC = `grep ^$mod /etc/passwd`;

6 REPLIES 6
Patrick Wallek
Honored Contributor

Re: call system command and store result in variable

REC=$(grep ^${mod} /etc/passwd)
A. Clay Stephenson
Acclaimed Contributor

Re: call system command and store result in variable

Well this is VERY hokey Perl -- why use grep when you already have regular expressions on steroids with Perl?

Your specific problem is that you need to strip your linefeed.

$mod = ;
chomp($mod);

You should also note that grep w/o -E doesn't understand "^" so lose the "^".

Finally, you should note that you might match more than one so you would be better served by assigning to an array but you would be far better served by writing this whole thing in Perl.

If it ain't broke, I can fix that.
Mike Keys
Regular Advisor

Re: call system command and store result in variable

I'm taking info I received that was written as a shell script and converting to Perl. Definitley not an expert and wasn't looking for someone to write it for me. Program is really a command line version of user administration in SAM.

Trying to put together a shell-driven menu that allows end user to add/modify/delete a user from /etc/passwd without ever having to touch that file.

I'll attach what I have thus far, so you guys can have a good laugh.
Roland Piette
Regular Advisor

Re: call system command and store result in variable

Mike,

I you want to search a string in a file use fgrep in place of grep.

I hope it help
Roland
Mike Keys
Regular Advisor

Re: call system command and store result in variable

Here's a snip of code. I'm trying to code the WHILE to look for name. If not EOF it goes to next line. If ID not found, I want it to print out a comment that says such. My problem is I don't know where to insert that comment.

print "Enter ID of user to modify: "; $mod = ; print "\n\n";
chomp($mod);
$passwd = "/etc/passwd";
open(PW,$passwd) or die "Can't open $passwd:$!\n";
while (){
($name,$passwd,$uid,$gid,$gcos,$dir,$shell) = split(/:/);
if (${mod} eq ${name}) {
print "${name} has the following values\n\n";
print "Password is: \t${passwd}\n";
print "User ID: \t${uid}\n";
print "Group ID: \t${gid}\n";
print "User name: \t${gcos}\n";
print "Home Directory: ${dir}\n";
print "Default shell: ${shell}\n";
system "/usr/bin/sleep 2";
&MainMenu;
}
elsif (${mod} ne ${name}) {
next;
}
}
close(PW); #Does it go here.
print "${mod} not found in /etc/passwd\n";
system "/usr/bin/sleep 2";
&MainMenu;
A. Clay Stephenson
Acclaimed Contributor

Re: call system command and store result in variable

Well, you are doing this the hard way. There is already a built-in getpwnam function that works just like its C counterpart.
Also don't use the curly braces around Perl variables --- Perl ain't shell.


print "Enter ID of user to modify: ";
$mod = ;
print "\n\n";
chomp($mod);

($name,$passwd,$uid,$gid,$quota,$comment,$gecos,$dir,$shell) = getpwnam($mod);
if (defined($name))
{
print "Okay\n"
}
else
{
print "User ",$mod," not found."
}

It would be a good idea to "man perlfunc | lp" to get a listing of the available functions.

If it ain't broke, I can fix that.