I need a bash script converted to perl

#!/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
mesdjeffersonAsked:
Who is Participating?
 
ozoCommented:
A fairly literal translation of the bash script, preserving what may be bugs, could look like:
#!/bin/perl

#while true;
#do
while( 1 ){
#clear
system "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"
print <<END;
=========================
      --- Menu ---
=========================
Enter 1 to Create Group:
Enter 2 to Remove Group:
Enter 3 to Create User:
Enter 4 to Remove User:
Enter 5 to Kill User Processes:
Enter q to exit the menu:


END
#echo -e "Enter Your selection \c"
print "Enter Your selection ";
#
#read choice
$choice=<>;
#case ${choice} in
use feature "switch";
use feature "say";
given( $choice ){
#
#1)
when( /^1$/ ){
#echo "Enter group name to add"
say "Enter group name to add";
#read newgroup
chomp($newgroup=<>);
#if egrep -i "$newgroup" /etc/group
#then
if( system qq(egrep -i "$newgroup" /etc/group) ){
#echo "Group already exists"
say "Group already exists"
#
}else{
#else
#groupadd $newgroup
system "groupadd $newgroup";
#echo "Group $newgroup added"
say "Group $newgroup added";
#fi
}
#;;
}
#
#2)
when( /^2$/ ){
#echo "Enter Group name to delete"
say "Enter Group name to delete";
#read delgroup
chomp($delgroup=<>);
#if egrep -i "$delgroup" /etc/group
#then
if( system qq(egrep -i "$delgroup" /etc/group) ){
#groupdel $delgroup
system "groupdel $delgroup";
#echo "Group $delgroup deleted"
say "Group $delgroup deleted";
#
#else
}else{
#echo "Group does not exist"
say "Group does not exist";
#fi
}
#;;
}
#
#3)
when( /^3$/ ){
#echo "enter User name to add"
say "enter User name to add";
#
#read newuser
chomp($newuser=<>);
#if egrep -i "$newuser" /etc/passwd
#then
if( system qq(egrep -i "$newuser" /etc/passwd) ){
#echo "User already exists"
say "User already exists";
#
#else
}else{
#useradd -m "$newuser"
system qq(useradd -m "$newuser");
#passwd $newuser
system "passwd $newuser";
#
# read -p "Does a group for new user exist?" yn
print "Does a group for new user exist?"; $yn=<>;
#    case $yn in
given( $yn ){
#        [Yy]* ) read -p "Please enter group name."; break;
when( /^[Yy]/ ){ print "Please enter group name."; chomp($REPLY=<>); break;
#read groupname
chomp($groupname=<>);
#useradd –G $groupname $newuser;
system "useradd –G $groupname $newuser";
# break;;
break}
#        [Nn]* ) read -p "Enter the name of new group."; echo
when( /^[Nn]/ ){ print "Enter the name of new group."; chomp($REPLY=<>) ; print;
#read newgroup
chomp($newgroup=<>);
#groupadd $newgroup
system "groupadd $newgroup";
#useradd –G $newgroup $user;
system "useradd –G $newgroup $user";
#
#esac
}}
#
#fi
}
#;;
}
#
#4) echo "Enter user name to be deleted"
when( /^4$/ ){ say "Enter user name to be deleted";
#read deluser
chomp($deluser=<>);
#if egrep -i "$deluser" /etc/passwd >/dev/null
#then
if( system qq(egrep -i "$deluser" /etc/passwd >/dev/null) ){
#userdel -rf "$deluser"
system qq(userdel -rf "$deluser");
#echo "User $deluser has been deleted"
say "User $deluser has been deleted"
#
#else
}else{
#echo "User does not exist"
say "User does not exist"
#fi
}
#;;
}
#
#5) echo "Which user processes need to be shutdown?"
when( /^5$/ ){ say "Which user processes need to be shutdown?";
#read user
chomp($user=<>);
#ps -aux | grep {user}
system "ps -aux | grep {user}";
#read -p "Confirm shutdown of running processes?" yn
print "Confirm shutdown of running processes?"; chomp($yn=<>);
#case $yn in
given( $yn ){
#        [Yy]* ) read -p "User processes being killed"
when( /^[Yy]/ ){ print "User processes being killed";  chomp($REPLY=<>);
#kill ps -aux | grep {user} | awk "{print $2}";;
system 'kill ps -aux | grep {user} | awk "{print $2}"'; }
#
#      [Nn]* ) echo "User not found. No processes will be killed"
when( /^[Nn]/ ){ say "User not found. No processes will be killed"; }
#esac
}
#
#;;
}
#
#q) exit
when( /^q$/ ){ exit; }
#esac
}
#
#echo -e "Enter Your selection \c"
print "Enter Your selection ";
#read input
chomp($input=<>);
#done
}
0
 
TintinCommented:
Why do you want to convert it to Perl when it will be mostly shelling out to do the real work?
0
 
mesdjeffersonAuthor Commented:
Well, I'm trying to teach myself Perl and chose a fairly simple bash script to compare the two. I just wanted to see how the look and feel of Perl flowed.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mesdjeffersonAuthor Commented:
I figured if I have a script I am comfortable with to compare it to would help me better understand it.
0
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Great that you want to learn, what have you programmed sofar for the above script?
0
 
mesdjeffersonAuthor Commented:
So far I have gotten just the first Input option to run correctly. I have been playing with this for days now and have started over a million times. I don't know why I'm not getting it. I know it can't be this difficult. It's definitely user error. This is what I have so far. Thanks for taking the time to help me.

#!/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 = `groupadd $newgroup`;
                                                                                                                                                            print "Adding group $newgroup...\n";}

else
{print "Can't create group\n"};
{yesorno;}
                                                                                                                                          

else ($input == 2)                                                                                                                                          {print "Please enter a group name to delete\n";
                                                                                                                                                
my $groupdel = `/usr/sbin/groupdel -g`;
              my $delgroup = <>;                                                                                                                  
chomp ($delgroup);
print "Do you want to delete $delgroup y/n?\n";
                                                                                                                  
$_ = <>;
chomp;                                          
$_ = "y"

if ($_ =~ /[Yy]/);
                                                                                                                                    
my $cmd = `groupdel $delgroup`;
                                                                                                                                    
print "Deleting group $delgroup...\n";}

else
{print "Can't delete group\n"};
{yesorno;}                  

exit      0;}
0
 
ozoCommented:
syntax error at Q_28636831.perl line 60, near "else"
Did you mean
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 = `groupadd $newgroup`;
        print "Adding group $newgroup...\n";}

else
{print "Can't create group\n"};
{yesorno;}


}elsif ($input == 2)        {print "Please enter a group name to delete\n";
0
 
mesdjeffersonAuthor Commented:
Thanks for your help! I've been playing around  with this script for a few days now and I'm starting to get a hang of the syntax. I'm going to start working on arrays next!  Do you have any helpful advice or sites that have video tutorials?
0
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.

All Courses

From novice to tech pro — start learning today.