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
PerlShell ScriptingLinux
Last Comment
mesdjefferson
8/22/2022 - Mon
Tintin
Why do you want to convert it to Perl when it will be mostly shelling out to do the real work?
mesdjefferson
ASKER
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.
mesdjefferson
ASKER
I figured if I have a script I am comfortable with to compare it to would help me better understand it.
Great that you want to learn, what have you programmed sofar for the above script?
mesdjefferson
ASKER
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;}
}
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?