
dustin.brown1
New User
Dec 14, 2015, 12:50 PM
Post #1 of 2
(1049 views)
|
Perl Scripting Help
|
Can't Post
|
|
Hey all, taking a class on UNIX Programming and going through a Perl phase at the moment. I have made the following script, but I am unable to get the add group and delete group sections to work. Can anyone provide some guidance for me? It is greatly appreciated.
#!/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( "groupdel", "$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 } The issue I get for adding a group is: Can't use string ("Test2") as a HASH ref while "strict refs" in use at ./example3 line 35, <> line 2. The error I get for deleting a group that I made previously in an earlier class, I get: Can't use string ("Test") as a HASH ref while "strict refs" in use at ./example3 line 44, <> line 2.
|