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.


In reply to unix to perl questions by deyaneria

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.