in reply to unix to perl questions
Using a subroutine and a hash will help take out some troubles.
Something in this like:
use warnings; use strict; my $choice; 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 { # do for groupadd }, 4 => sub { # do for groupdel }, 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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: unix to perl questions
by deyaneria (Acolyte) on Mar 12, 2015 at 17:25 UTC |