CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Creating Users/Groups

 



stevenj0728
New User

Dec 9, 2012, 7:53 AM

Post #1 of 5 (1269 views)
Creating Users/Groups Can't Post

I running into a block with a script that i need to create. I need to create a user or a group. The problem is that if the user prompted for the tasks in a menu based options and they pick user first, I need to also have them specify a group, but what if one isnt created? How can I redirect them to create a group while making a user?



I am using this to start out with:




print "\n Maintenance Tasks Menu \n
1 - Create UNIX Group
2 - Drop UNIX Group
3 - Create User
4 - Drop User
\n
Please enter the task number you wish execute \n" ;





Then I plan on using a case statement for these options. I also what to throw in a loop here, I believe, so that if I user inputs another value than 1,2,3,or 4 it will throw them back into the menu to choose again.

I have this for my else statement, but not sure how to continue the script when its complete.



else { print "Invalid task number, please try again\n"; }


Laurent_R
Veteran / Moderator

Dec 9, 2012, 9:48 AM

Post #2 of 5 (1268 views)
Re: [stevenj0728] Creating Users/Groups [In reply to] Can't Post

I think that your menu needs at least one additional option: an exit option.

Otherwise, as for going through the group creation option if the group chosen is not yet created when creating a user, you would have to show us more of your code so that we can tell you how to amend it.

But, basically, if each of the options executes a specific subroutine, all you have to do is to call the group creation subroutine when needed within the user creation option.


stevenj0728
New User

Dec 9, 2012, 10:21 AM

Post #3 of 5 (1267 views)
Re: [Laurent_R] Creating Users/Groups [In reply to] Can't Post

It seems that everything works. The program complains about the break in case statement being unquoted. How can I loop this application so that after completing a task except exiting it will prompt the menu again?


#!/usr/bin/perl -w

#use strict;
use warnings;
use Switch;



print "\n Maintenance Tasks Menu \n
1 - Create UNIX Group
2 - Drop UNIX Group
3 - Create User
4 - Drop User
5 - Exit
\n
Please enter the task number you wish execute \n" ;


my $user_input = <> ;
chomp ($user_input) ;
switch($user_input)
{
case 1 { print "Enter group name to create\n";
my $user_input = <>;
chomp ($user_input);
system("groupadd $user_input"); break; }
case 2 { print "Enter group name to delete\n";
my $user_input = <>;
chomp ($user_input);
system("groupdel $user_input"); break; }
case 3 { print "Enter user name to create\n";
my $user_input = <>;
chomp ($user_input);
system("useradd $user_input"); break; }
case 4 { print "Enter user name to delete\n";
my $user_input = <>;
chomp ($user_input);
system("userdel $user_input"); break; }
case 5 { print "You are now exiting the application\n";
exit; break;}

else { print "Invalid task number, please try again\n"; }
}


(This post was edited by stevenj0728 on Dec 9, 2012, 11:26 AM)


Laurent_R
Veteran / Moderator

Dec 9, 2012, 3:17 PM

Post #4 of 5 (1260 views)
Re: [stevenj0728] Creating Users/Groups [In reply to] Can't Post

What are you trying to do with this break?

I have never used the "switch... case" construct in real life programming (only tried it a couple of time for the sake of testing it), so I may be wrong on this. However, I think there is no break statement needed here (and especially not after the exit).

My understanding is that the Perl "switch... case" construct does not allow fall-through by default (although you can do it by adding a next statement, but this is rarely a good idea), so no break is necessary as in some other languages.


FishMonger
Veteran / Moderator

Dec 10, 2012, 6:18 AM

Post #5 of 5 (1251 views)
Re: [stevenj0728] Creating Users/Groups [In reply to] Can't Post

The first thing you need or should do is to uncomment the 'use strict' statement. Ignoring the problems that it points out is not a good idea.

The break statement is used to break out of a given/when block, not a switch/case block.

You really shouldn't be using the Switch module. It's a source filter module and is known to be buggy and because of this, it has been removed from the perl core (as of v5.12 or v5.13).

A given/when block could be used in this case, but my preference would be to use a dispatch table.


Quote
How can I loop this application so that after completing a task except exiting it will prompt the menu again?

Just wrap your menu code in a while(1) { ... } block.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives