I am new to Perl and can't figure out how to get the groupadd to work in my script.

I have the same script in bash but was playing with it in Perl but can't get it.

Bash Script

Open in new window


#!/bin/bash

while true;
do
clear
echo "========================="
echo "      --- Menu ---"
echo "========================="
echo "Enter 1 to Create Group: "
echo "Enter 2 to Remove Group: "
echo "Enter 3 to Create User: "
echo "Enter 4 to Remove User: "
echo "Enter 5 to Kill User Processes: "
echo "Enter q to exit the menu: "
echo -e "\n"
echo -e "Enter Your selection \c"

read choice
case ${choice} in

1)
echo "Enter group name to add"
read newgroup
if egrep -i "$newgroup" /etc/group
then
echo "Group already exists"

else
groupadd $newgroup
echo "Group $newgroup added"
fi
;;

2)
echo "Enter Group name to delete"
read delgroup
if egrep -i "$delgroup" /etc/group
then
groupdel $delgroup
echo "Group $delgroup deleted"

else
echo "Group does not exist"
fi
;;

3)
echo "enter User name to add"

read newuser
if egrep -i "$newuser" /etc/passwd
then
echo "User already exists"

else
useradd -m "$newuser"
passwd $newuser

 read -p "Does a group for new user exist?" yn
    case $yn in
        [Yy]* ) read -p "Please enter group name."; break;
read groupname
useradd –G $groupname $newuser;
 break;;
        [Nn]* ) read -p "Enter the name of new group."; echo
read newgroup
groupadd $newgroup
useradd –G $newgroup $user;

esac

fi
;;

4) echo "Enter user name to be deleted"
read deluser
if egrep -i "$deluser" /etc/passwd >/dev/null
then
userdel -rf "$deluser"
echo "User $deluser has been deleted"

else
echo "User does not exist"
fi
;;

5) echo "Which user processes need to be shutdown?"
read user
ps -aux | grep {user}
read -p "Confirm shutdown of running processes?" yn
case $yn in
        [Yy]* ) read -p "User processes being killed"
kill ps -aux | grep {user} | awk "{print $2}";;

      [Nn]* ) echo "User not found. No processes will be killed"
esac

;;

q) exit
esac

echo -e "Enter Your selection \c"
read input
done

Perl Script (just a piece):

#!/usr/bin/perl

use strict;
use warnings;
use Cwd qw();

sub yesorno
      {YESNO:
      print "Return to main menu? [Y/N]";
      my $return = <>;
      chomp($return);
if (($return eq "Y") || ($return eq "y"))
      {goto START;}
elsif (($return eq "N") || ($return eq "n"))
      {exit;}
else {print "Invalid selection, please try again\n";
      goto YESNO;}
}

START:

my $input = "";

do
{

    print "1. Create Group:\n";
    print "2. Remove Group:\n";
    print "3. Create User:\n";
    print "4. Remove User:\n";
    print "5. Kill User Processes:\n";
    print "6. exit\n";

    print "Enter your choice: ";
    $input = <>;
    chomp($input);
                                                                                                                                                                                                                                                                        
if ($input == 1)                                                                                                      

    {print "Please enter a group name to create\n";                                                                  
my $groupadd = '/usr/sbin/groupadd -g';
my $newgroup = <>;                                                                                                            
chomp ($newgroup);                                                                                                
print "Do you want to create $newgroup y/n?\n";

$_ = <>;
chomp;
$_ = "y"                                                                                                                         

if ($_ =~ /[Yy]/);

my $cmd = (addgroup      $newgroup);
     print "$cmd\n";                                                                                                    
     print "Adding group $newgroup...\n";}
                                                                                                                                
else
{print "Can't create user\n"};
{yesorno;}                                                                                                                                          
exit 0;
}
mesdjeffersonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
my $cmd = `addgroup      $newgroup`;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mesdjeffersonAuthor Commented:
Thank you for your quick response. This really helped me!! I can't believe it was as simple as that.  Would it be safe to assume that creating a user and adding them to a group would be just as simple?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.