Group Doesn't Exist

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

I'm able to create a group but when I'm trying to delete the group it keeps stating Group Doesn't Exist. I know the group exist since I created it and look at the etc/group file which is there but for some odd reason I'm unable to delete it???

  1. Relevant commands, code, scripts, algorithms:
#!/usr/bin/perl

use strict;
use warnings;

my $group_name = <STDIN>;
my $group_id = <STDIN>; 
my $user_name = <STDIN>;
my $user_id = <STDIN>;
my $choice = <STDIN>;

print "* My menu *\n";
print "* *\n";
print "* 1. Create a Unix Group  *\n";
print "* 2. Delete a Unix Group  *\n";
print "* 3. Create a Unix User *\n";
print "* 4. Delete a Unix USer *\n";
print "* 5. Quit *\n";
print "* *\n";
print "******************************************************\n";
print "* Enter Your Choice >\n";

$choice = <STDIN> ;

chomp $choice;
if ($choice == 1) {
  print "**********************************\n";
  print " Create A Unix Group\n";
  print "**********************************\n";
  print " Enter The Group Name to Create >\n";
  chomp ($group_name = <STDIN>);
  print " Enter the Group ID to Create >\n";
  chomp ($group_id = <STDIN> );

  if ( ! `grep -i $group_name /etc/group` ) {
    system ("/usr/sbin/groupadd $group_id $group_name");
    print "Group Created Successfully!\n"
  } else {
    print "Group Already Exists !\n";
  }
}


elsif ($choice == 2) {
  print "**********************************\n";
  print " Delete a Unix Group\n";
  print "**********************************\n";
  print " Enter a Group Name to Delete >\n";
  chomp ($group_name = <STDIN>);
  print " Enter a Group ID to Delete >\n";
  chomp ($group_id = <STDIN> );

  if ( ! `grep -i $group_name /etc/group` ) {
    system ("/usr/sbin/groupdel -g $group_id $group_name");
    print "Delete Group Successfully!\n"
  } else {
    print "Group Doesn't Exist !\n";
  }
}

chomp $choice;
if ($choice == 3) {
  print "**********************************\n";
  print " Create a Unix User\n";
  print "**********************************\n";
  print " Enter User Name You Want to Create >\n";
  chomp ($user_name = <STDIN>);
  print " Enter the User ID >\n";
  chomp ($user_id = <STDIN>);

  if ( ! `grep -i $user_name /etc/passwd` ) {
    system ("/usr/sbin/useradd -m $user_id $user_name");
    print "Added User Successfully!\n"
  } else {
    print "User Doesn't Exist !\n";
  }
}
  1. The attempts at a solution (include all code and scripts):

Tried to get some advice from stack exchange guys but no luck. I tried changing variables but that didn't help.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

CTU Online, N Chestnut St, Colorado Springs, CO 80907, Instructor Tarik Illes Course CS345

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

groupdel (On Linux and some other UNIXES ) does not have a -g option. Your code is incurring an error and failing; note you are interpreting it as the 'group does not exist'.

Commands can fail dozens of ways. Read the info or man page for the allowed options for your flavor of UNIX. And trap all of the errors you can so your code is not misleading, simply say something like 'groupdel failed.' To do this you need to redirect stderr in your code to a variable or to stdout. In other words, display the error message groudel or goupadd emits.

Hi dude,
the actual problem is the wrong "if"-statement, one line before the "groupdel" command.
Due to the exclamation mark, it reads like "If the group ... is NOT found, delete it",
but since the group does exist, the else part of that if statement gets triggered.
So you got to remove the exclamation mark (!) in the line 53.

I guess that happened because you copied it from the groupadd section.

Here are some further suggestions:

  1. imho you don't need to read from STDIN at the program start, so try this instead:
my ($group_name, $group_id, $user_name, $user_id, $choice);
  1. There is a "-g" option missing between the groupadd command and $group_id

  2. groupdel requires groupname as argument, so probably you should remove -g $group_id (see jim's answer)

  3. you should not simply system("some-command") and then independently print some status or return code. Try this:

system ("/usr/sbin/groupadd -g $group_id $group_name && echo 'Success!' || echo 'Error!'");
  1. if ($choice == 1) ... elsif ($choice == 2) ... if ($choice == 3) seems not to be consistent

  2. If your perl script still misbehaves, consider using the perl debugger: perl -d yourscript.pl

HTH, good luck!

1 Like