deyaneria has asked for the wisdom of the Perl Monks concerning the following question:
I'm a student working on Perl program the class had us write an Admin Menu in week two that did the following below, for week 4 we are to write it in Perl. I've had two weeks of unix and 1 week of Perl. I have spent hours looking for the correct syntax to change my working programn in UNIX to Perl Script.I want to stress I don't want homework script answers I just need a bit of help with syntax and what commands to use in this new language. I've taken other languages COBOL, C+ and most recently java and html but still trying to find similarities so I can connect the dots. The assignment is:Convert your second shell script (User Maintenance) into a Perl script. This Perl script must perform the following tasks: • Generate a menu to ask the system administrator for the task that he or she would like to see performed. • The available tasks are as follows: o Create a UNIX group. o Drop a UNIX group. o Create a user. o Drop a user. • Prompt the user for the choice, and perform the system command. • In addition, check to make sure that before creating either the user or the group that neither already exists; if it does already exist, print out the existing user ID, comment field, and home directory locations from the password file. Here is the code I have written thus far. I figured if I could get the first part working that the others would fall in place.
#! /usr/local/bin/perl –w #Author: # $choice = 0; Do{ Print “Admin Menu \n”; Print “To add a user, press 1 \n”; Print “To delete a user, press 2 \n”; Print “To Add a group, press 3 \n”; Print “To delete a group, press 4 \n”; Print “to exit this menu, press 5 \n”; Print “Please chose an option:” $choice =<>; chomp $choice; Print “You chose option: “; Print $choice; Print “\n”; } while $choice < 1 || $choice > 5; If ($choice == 1); { Do { nUsername = <>; Print “what is new users name?”; #get new user name Chomp $nUsername; #check that new user name is a string If $nUsername =~ tr/A-Za-z0-9//c; { #check to see if user exists if(getpwnam('$nUsername')) print "User exists\n"; } Else { $adduser = '/usr/sbin/adduser'; $cmd = qq($adduser --home $home --gecos $nUsername); #get new user password Do { Print “Please add password: \n”; $nPassword = <>; Print “Please re-enter password: \n”; $nPasstest =<>; } While $nPassword !eq $nPasstest; Print “Please enter group for user: \n”; nGroup= <>; #add user to group if getgrnam($group) { #check to see is group exists print $group; Print “already exists \n”; #add user to group Groupadd $nGroup; } Else {#if group does not exist create group groupadd $nGroup; Print $nGroup; Print “ has been added.\n”; } Else { Print “New username must be a string.”; $nUsername = \n; } while $nUsername = \n;
I tried to follow all recommendations for posting here. My apologies if I messed up the posting rules. I wanted to add what I am particularily looking for is the correct command to add a new group. However any other help would be appreciated. I am usng oracle vm with Ubuntu as a shell.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: unix to perl questions
by 2teez (Vicar) on Mar 11, 2015 at 23:55 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 00:18 UTC | |
by Anonymous Monk on Mar 12, 2015 at 01:12 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 03:01 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 02:48 UTC | |
by 2teez (Vicar) on Mar 12, 2015 at 02:24 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 02:51 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 03:10 UTC | |
Re: unix to perl questions
by Anonymous Monk on Mar 12, 2015 at 00:03 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 00:25 UTC | |
by GrandFather (Saint) on Mar 12, 2015 at 08:56 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 16:50 UTC | |
by GrandFather (Saint) on Mar 12, 2015 at 20:23 UTC | |
| |
Re: unix to perl questions
by Anonymous Monk on Mar 12, 2015 at 00:00 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 00:21 UTC | |
Re: unix to perl questions
by Anonymous Monk on Mar 12, 2015 at 02:42 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 02:59 UTC | |
by soonix (Chancellor) on Mar 13, 2015 at 10:28 UTC | |
by Anonymous Monk on Mar 14, 2015 at 00:24 UTC | |
by deyaneria (Acolyte) on Mar 13, 2015 at 16:46 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 03:07 UTC | |
by dsheroh (Monsignor) on Mar 12, 2015 at 15:49 UTC | |
by Anonymous Monk on Mar 12, 2015 at 16:41 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 16:56 UTC | |
by choroba (Cardinal) on Mar 12, 2015 at 16:57 UTC | |
Re: unix to perl questions
by Anonymous Monk on Mar 12, 2015 at 07:36 UTC | |
by deyaneria (Acolyte) on Mar 12, 2015 at 17:25 UTC |