dustin.brown1 has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use warnings; use strict; my $user_grp = read_doc("/etc/passwd"); my %admin_menu = ( 1 => sub { my $username = get_input("Enter username: "); if ( exists $user_grp->{$username} ) { print "\nUser ", $username, " exist!"; return; } system( "useradd", "-u $$", "-g", "users", "$username" ); }, 2 => sub { my $username = get_input("Enter username: "); if ( exists $user_grp->{$username} ) { system( "userdel", "$username" ) if get_input("Do you really want to remove the $username ?: ") =~ /\by\b/i; } }, 3 => sub { my $group_name = get_input("Enter Group Name: "); if ( exists $group_name->{$group_name} ) { print "\nUser ", $group_name, " exist!"; return; } system( "groupadd -g $group_id $group_name" ); }, 4 => sub { my $group_name = get_input("Enter Group Name: "); if ( exists $group_name->{$group_name} ) { system( "delgroup", "$group_name" ) if get_input("Do you really want to remove the $group_na +me ?: ") =~ /\by\b/i; } }, 5 => sub { get_input("Exiting the Program\n"); exit(); }, ); do { admin_menu(); $choice = get_input("Enter your choice: "); if ( $choice < 1 or $choice > 5 ) { admin_menu(); } else { $admin_menu{$choice}->(); } } while (1); sub get_input { my $prompt = shift; print $prompt; chomp( my $value = <> ); return $value; } sub read_doc { my $file = shift; my %user; open my $fh, '<', $file or die $!; while (<$fh>) { my ( $name, $grp ) = ( split /:/ )[ 0, 4 ]; $user{$name} = $grp; } return \%user; } sub admin_menu { print << "UNIX_ADMIN"; Admin Menu To Add a User, press 1: To Delete a User, press 2: To Add a group, press 3: To Delete a group, press 4: To Exit this Menu, press 5: UNIX_ADMIN }
Update: Everything is fixed now. Thanks so much!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Scripting Help
by NetWallah (Canon) on Dec 14, 2015 at 19:26 UTC | |
by dustin.brown1 (Initiate) on Dec 14, 2015 at 20:13 UTC | |
by NetWallah (Canon) on Dec 14, 2015 at 20:48 UTC | |
by dustin.brown1 (Initiate) on Dec 14, 2015 at 21:11 UTC | |
by NetWallah (Canon) on Dec 14, 2015 at 21:54 UTC | |
|
Re: Scripting Help
by RichardK (Parson) on Dec 14, 2015 at 19:19 UTC | |
by Anonymous Monk on Dec 14, 2015 at 19:48 UTC | |
by mr_ron (Deacon) on Dec 14, 2015 at 20:40 UTC | |
by poj (Abbot) on Dec 14, 2015 at 20:32 UTC | |
by dustin.brown1 (Initiate) on Dec 14, 2015 at 21:13 UTC | |
by poj (Abbot) on Dec 14, 2015 at 21:19 UTC |