Group Doesn't Exist

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Group Doesn't Exist
# 1  
Old 08-02-2014
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???

2. Relevant commands, code, scripts, algorithms:
Code:
#!/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";
  }
}

3. 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.

4. 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).

Last edited by vbe; 08-02-2014 at 06:05 PM..
# 2  
Old 08-02-2014
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.
# 3  
Old 08-05-2014
Bug

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:
Code:
my ($group_name, $group_id, $user_name, $user_id, $choice);

2. There is a "-g" option missing between the groupadd command and $group_id

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

4. you should not simply system("some-command") and then independently print some status or return code. Try this:
Code:
system ("/usr/sbin/groupadd -g $group_id $group_name && echo 'Success!' || echo 'Error!'");

5. if ($choice == 1) ... elsif ($choice == 2) ... if ($choice == 3) seems not to be consistent

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

HTH, good luck!
This User Gave Thanks to junior-helper For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

2. Red Hat

Removing LVM Volume Group that doesn't exist anymore

Our SAN administrator decided to unpresent then destroy LUN's we were actively using as a volume group (all PV's in said volume group). Now every time I do a pvscan or whatever it complains about I/O errors trying to access those PV's. How do I get it to forget the VG existed completely? vgreduce... (7 Replies)
Discussion started by: thmnetwork
7 Replies

3. Solaris

User directory doesn't exist

Hii all, i create the user useradd -d /home/kk kk passwd kk when i tried to login to kk i get a error user directory doesn't exist then i tried useradd kkk passwd kkkwhen i tried to login to kkk i get the same error user directory doesn't exist. (4 Replies)
Discussion started by: vipinkumarr89
4 Replies

4. UNIX for Dummies Questions & Answers

Removing a user that doesnt exist from a group

Hi there, normally if I want to remove a user tht I have added to a specific group, i would do the following this is what my group2 looks like # grep group2 /etc/group group2:x:7777:user2,user1,user4 user1 has been defined in a few groups # id -nG user1 group1 group2 group3 So... (3 Replies)
Discussion started by: rethink
3 Replies

5. Programming

Kernel module - How to test if file doesn't exist

Hey, I'm currently getting into some kernel module progamming. As a little exercise I want to read the headers out of an ELF file. My code is very simple, here is the important part: struct file *fp; /* ... */ fp = filp_open("some/file/on/my/pc", O_RDONLY, 0); if(fp == NULL) { ... (15 Replies)
Discussion started by: disaster
15 Replies

6. Shell Programming and Scripting

Perl: One action if an element doesn't exist in array

Hello, I want to run one (not multiple) action if an element doesn't exist in array. for example: @array = (1..10); foreach $el (@array) { if ($el != 11) { print "number not found\n"; } } the output of this simple script: number not found (3 Replies)
Discussion started by: ahmed_zaher
3 Replies

7. Shell Programming and Scripting

Group Exist Scripting

Hey People, I've got a question! How can i write a function in a script which is looking for if a group exist and if not, that the group "users" is the standard group..I know that i have to use "grep" und "if-else"..I will be very happy for answers ;) Greetz Ali (2 Replies)
Discussion started by: AliC
2 Replies

8. Shell Programming and Scripting

sftp mget where file doesn't exist BASH

I have a script that is working: #!/bin/bash sftp user@domain.com <<EOF cd somedir mget *.csv quit EOF but on a crontab I want to only pull newer files, so I want to do something like: while read ls current dir local file != true do mget that new file but I'm not sure the syntax... (2 Replies)
Discussion started by: unclecameron
2 Replies

9. UNIX for Advanced & Expert Users

Validate if user and group exist

I'm kinda new to unix programming so bear with me... I'm running a script prompting a user for an existing user and group and want to be able to validate if they valid. Is there any code available? Any help or push in the right direction would help. Thank you, (2 Replies)
Discussion started by: thedon
2 Replies

10. UNIX for Dummies Questions & Answers

What hapens if a group member doesn't exist?

As part of a NIS implementation, (I think) I want to create a group (in /etc/group) that has users that do not exist on the target machine. What effect will this have? Will it cause any problems? Thanks, Gary Cooper (1 Reply)
Discussion started by: Gary Cooper
1 Replies
Login or Register to Ask a Question