in reply to unix to perl questions

Not beautiful, but better, I hope:
#!/usr/local/bin/perl use strict; use warnings; use Term::ReadKey qw(ReadMode ReadLine); use IPC::System::Simple qw(system); my $choice=0; GETCHOICE: { 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 choose an option: "; chomp($choice=<STDIN>); redo GETCHOICE if $choice < 1 || $choice > 5; } print "You chose option: $choice\n"; if($choice == 1) { my $nUsername; GETUSER: { print "What is new users name? "; chomp($nUsername=<STDIN>); # check that new user name is a string if($nUsername!~/^[A-Za-z0-9]+$/) { print "New username must be a string.\n"; redo GETUSER; } } # check to see if user exists if(defined getpwnam($nUsername)) { print "User exists\n"; } else { ## FIXME delete "echo" system("echo","/usr/sbin/adduser","--home", "/home/$nUsername","--gecos",$nUsername); # get new user password my ($nPassword,$nPasstest); GETPASS: { ReadMode("noecho"); print "Please add password: "; chomp($nPassword=ReadLine(0)); print "\n"; print "Please re-enter password: "; chomp($nPasstest=ReadLine(0)); print "\n"; ReadMode("restore"); redo GETPASS if $nPassword ne $nPasstest; } print "Please enter group for user: "; chomp(my $nGroup=<STDIN>); # add user to group if(defined getgrnam($nGroup)) { # check to see is group exists print "$nGroup already exists\n"; # add user to group system("echo","groupadd",$nGroup); } else { # if group does not exist create group system("echo","groupadd",$nGroup); print "$nGroup has been added.\n"; } } }

Replies are listed 'Best First'.
Re^2: unix to perl questions
by deyaneria (Acolyte) on Mar 12, 2015 at 02:59 UTC
    What is echo? I know we use this in UNIX in place of print. I see that you chose to use STDIN and using ReadLine(0) is that an array index? So you are testing changing nPassword to array ReadLine(0) and comparing it to nPassTest? Just want to be sure I am understanding correctly.
      system("echo",...) is roughly equivalent to print.
      I guess the reason why our anonymous brother didn't use the latter is that you have the "real" commands for debugging, and to change from "dry run" to "wet run" (or how this may be called) afterwards you do (in vi command mode)
      :%s/system("echo",/system(/
      BTW for vi on Windows, see http://www.vim.org/ugrankar.pdf
        Thank you for answering the echo question and for the link. I may consider putting another program on the comp. I'm just very careful as many of my classes require me to put programs on it already(every 5 weeks I end up having to add at least one new program for a class). I try to keep it simple.
        I guess the reason why our anonymous brother didn't use the latter is that you have the "real" commands for debugging, and to change from "dry run" to "wet run"
        Correct!
Re^2: unix to perl questions
by deyaneria (Acolyte) on Mar 12, 2015 at 03:07 UTC
    I feel like am stalking you...lol Really I am fascinated by the code. Getchoice would be function or subroutine? This eliminates the Do-While loop.I've also never seen "defined" what does that do exactly is it another way of doing a comparison?
      defined does pretty much exactly what the English word suggests: It tests whether the variable has a defined value, which is to say, whether any value has ever been assigned to the variable.

      In this particular case, if(defined getpwnam($nUsername)) checks to see whether getpwnam returns a value at all:
      - If it finds the user, it will return something and "something" is always defined, so the test evaluates as true.
      - If it doesn't find the user, it will return nothing (not just an empty value or a false value, but the absence of a value - in Perl, this not-a-value is called undef, for "undefined"), and the test evaluates as false.

        Just to add to that: The case where the function returns a "defined but false" value is the user "root", who has a UID of 0.
        Is "undefined" similar to null in Perl? I have yet to see null in Perl.