Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Want to Create Unix group using perl script

by abhi.nitk2003 (Initiate)
on Mar 17, 2010 at 13:16 UTC ( #829170=perlquestion: print w/replies, xml ) Need Help??
abhi.nitk2003 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am new to Perl,I want to write a perl sub routine inside a existing script which must run as a "root" and then create a Unix group if does not exists. if it exist it print the message the group already exists. and then it will do a chgrp -R ,chmod g+s ..commands on the mentioned directory. Please help
  • Comment on Want to Create Unix group using perl script

Replies are listed 'Best First'.
Re: Want to Create Unix group using perl script
by merlyn (Sage) on Mar 17, 2010 at 15:17 UTC
    The way to create a "Unix" group varies widely (and Perl can't do it directly). You need to define which flavor of "Unix" you're dealing with. (I'm saying "Unix" in quotes, because Linux isn't Unix, and that's one possibility.)

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: Want to Create Unix group using perl script
by almut (Canon) on Mar 17, 2010 at 14:01 UTC

    For checking if a group exists you can use getgrnam (scalar getgrnam("foo") — returns the GID, or undef if a group of the given name doesn't exist).

    For creating a new group, I'd use the respective command supplied by your system, such as groupadd (call it via system) — there's no reason to reinvent the wheel and run into various gotchas that can be avoided by using existing tools.

      It seems groupadd already does everything the OP wants: if the group doesn't exist, it adds it - if it does exist, it prints a message it already exists.

      Looks to me the OP just wants a three line shell script. Doesn't seem worth the trouble of writing a Perl wrapper for that.

      Thanks for the help guys it worked for me.
Re: Want to Create Unix group using perl script
by sierpinski (Hermit) on Mar 17, 2010 at 14:44 UTC

    To have the script run as root, you either need to be root when you execute it (or from root's crontab), or use sudo when the script is executed.

    As almut said, use system commands for most of the work. Just plan out how you would do it on the command line, then wrap those commands in Perl using one of the many methods (system, exec, backticks)... which one you use depends on various things, like do you want the script to wait for that command to finish before continuing, etc. You can read up on those commands just about everywhere. The easiest way is to google 'perl <command>'.

    /\ Sierpinski
      Hi, I got it. but I want my script just take group id (not group name) and if it exists then run chgrp,chmod g+s command as root.Basically my script is running under my cronjob but to run chmod , chgrp I need to become "root" first. so how should I do that
      abhishek~/perl ~> cat #!/usr/bin/perl $in=<STDIN>; ($name, $passwd, $gid, $members) = getgrgid($in); print "group name: $name \t group id: $gid \n"; if ( $in == $gid ) { print "Success\n"; #how to become "root" here.. exec ("chgrp -R $gid /home/abhishek/cpp") || die "could not do chgrp $ +!"; } else { print "Group id does not exists, you need to give the existing group i +d \n"; } abhishek~/perl ~>

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://829170]
Approved by Ratazong
Discipulus seems a part of the globe very unfrindly with Kawasaki riders..
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)
[perldigious]: "for how I roll"... ahhh, I see what you did there stevieb.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (14)
As of 2016-12-06 16:52 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.