deyaneria has asked for the wisdom of the Perl Monks concerning the following question:

Thank to the lovely monks help I have been working out my coding project for the week. However I have run into an issue regarding groups. I have been using the following code to add a group. It seems the group is not being added even though I get no errors when running the code. After I add a group I've been leaving the script and checking to see if group has been added in the terminal using the cat /etc/group command. I don't see it added. Any ideas why? I apologize for any mistakes word might have put in I can't seem to copy and paste from the shell. I am using oracle vm with Ubuntu and perl version 5.18.2 I am also using warning and strict and have no errors coming up.

3 => sub{ # do groupadd my $groupName = get_group (“Please enter new group name: “); if (getgrnam($groupName)) { print “Group already exists.”; } else { #if group does not exist create group ## FIXME delete “echo” system (“echo” , “groupadd”, “$groupName”); print “$groupName has been added. \n”; } }, sub get_group{ my $group = shift; print $group; chomp (my $ngroup = <>); return $nGroup; }

As I have to call for groups several times during the program I created a subroutine for it.

Replies are listed 'Best First'.
Re: adding a group to the Unix System
by hdb (Monsignor) on Mar 15, 2015 at 17:48 UTC

    Your command system (“echo” , “groupadd”, “$groupName”); only prints the UNIX command to add a group. The comment above that line suggests to delete "echo" from that line which should then lead to the execution of the command.

      thank you, I thought the ## was different than a comment and that it was only one # for a comment...I sent the same code to my teacher and he didn't see it either. I will correct immediately.
      And again thank you it's now working properly now I can fix the adduser part of the menu where it adds the user to a group.
Re: adding a group to the Unix System
by eyepopslikeamosquito (Archbishop) on Mar 15, 2015 at 20:56 UTC

    Not specifically answering your question, but be aware that Perl has a number of built-in functions relating to users and groups, for example: getlogin, getpwnam, getgrent. See Perl functions for fetching user and group info.

    And it is usually preferable to use the Perl builtin function rather than an external command (for why, see Unix shell versus Perl).

    Though I am not aware of a Perl builtin function that can add a Unix group, you could use them to help you verify the changes you make. To illustrate, the test program below prints all groups on the system along with which users are in each group:

    use strict; use warnings; my $user = getlogin(); my ($uid, $primarygid, $home, $shell) = (getpwnam($user))[2,3,7,8]; defined($uid) or die "$0: error user '$user' does not exist\n"; print "user='$user' primary gid=$primarygid\n\n"; while (my ($name, $pw, $gid, $members) = getgrent) { print "gid : $gid\n"; print "group name : $name\n"; print "members : $members\n\n"; } endgrent();

      Thanks for the link...I got the groups being added now just need to figure out how to add a new user to an existing group and a new group. I'm hoping your link might help with that.I've had 2 weeks of UNIX and 2 weeks of Perl now in the one class. It seems to be kicking my butt:) But I will plow through:)
Re: adding a group to the Unix System
by RichardK (Parson) on Mar 15, 2015 at 18:00 UTC

    Here are a few things for you to try :-

    1. Does the echo show anything?
    2. What happens when you remove echo from your command string?
    3. Do you have the correct permissions to run groupadd ?
    4. what does system return ?

    Try the suggestions in Basic debugging checklist to work out what's going wrong.

      The echo just did what it was suppose to do and printed the parts but taking it out fixed the issue now I can see that the group was created when I leave the script and cat /etc/group. This was only a part of the entire program I had to write. However it was only part not working properly per the parameters of the project.