Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Scripting Help

by dustin.brown1 (Initiate)
on Dec 14, 2015 at 18:35 UTC ( [id://1150263]=perlquestion: print w/replies, xml ) Need Help??

dustin.brown1 has asked for the wisdom of the Perl Monks concerning the following question:

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( "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
    Your "groupadd" code uses $group_id which does not seem to be defined.

    The linux command for deleting groups is "groupdel", not "delgroup".

    Error-checking Linux return codes (use $? >> 8) would certainly help.

            "I can cast out either one of your demons, but not both of them." -- the XORcist

      I corrected the groupdel part. Not sure where group_id should be defined.
        Just take out the "-g $group_id" from the groupadd command.

        Let the system generate a new gid for you.

                "I can cast out either one of your demons, but not both of them." -- the XORcist

Re: Scripting Help
by RichardK (Parson) on Dec 14, 2015 at 19:19 UTC

    In what way doesn't it work? do flames shoot out of the back of your computer or something else?

    Try the basic debugging checklist for some general help on how to fix anything.

    Also you could try checking the return values from the system calls.

    or step through it one line at a time in the debugger.

      Sorry I have been so frustrated with it after working on it for 6 hours lol. 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.

        In two places you repeat the code:

        my $group_name = get_input("Enter Group Name: "); if ( exists $group_name->{$group_name} ) {

        The sub get_input() returns a string which, as the error messages are telling you, you are trying to use as hash reference with exists. On the left side of the -> arrow $group_name is a hash reference and on the right side of the arrow it is a string and key to the hash. It looks like in read_doc you need to create a hash of groups and return a reference to it.

        Ron
        For the code you posted I get
        Global symbol "$group_id" requires explicit package name .. Global symbol "$choice" requires explicit package name ..
        poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1150263]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-03-28 12:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found