Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi All monks, I had posted an earlier question related to this one, but forgive me in advance because now I want to use @ARGV and not <>. So I want to run the script with the same functionality as below (userA mapped to userB's .profile and or userA mapped to userB's profile and userC mapped to userD's .profile and or userE and userF mapped to userG's .profile) like:

$ perl script -userA -userB

or $ perl script -userA -userB -userC -userD

or $ perl script -userE -userF -userG

as opposed to $ perl script then typing in the userA userB, CNTRL-D, userC userD

thanks!

#!/usr/bin/env perl use strict; use warnings; my (%adds, $user, $muser,); LOOP:while (<>) { chomp; ($user, $muser) = map lc, split; if ( s/[a-z]//g <= 5 ) { print uc("\nyou must enter at least 6 characters, try again!!!?? +?\n\n"); goto LOOP; } else { if ( -d "/users/$muser" ) { $adds{$user} = qx(ls /users/$muser/.profile); } elsif ( -d "/home/$muser" ) { $adds{$user} = qx(ls /home/$muser/.profile); } else { print "\n$muser does not have a /users subdir, exiting!\n\n"; exit 0; } print uc("\nnow building user associations for your user-adds!\n"); while ( my ($key, $val) = each(%adds) ) { print "\n$key will have a profile of $val\n"; chomp ($key,$val); if ( ! -d "/users/$key" ) { system("mkdir /users/$key"); system("chown $key:users /users/$key"); system("ln -sf $val /users/$key/.profile"); } } }

Replies are listed 'Best First'.
Re: switching from <> to $ARGV
by ikegami (Patriarch) on May 20, 2010 at 02:27 UTC

    Fixed numerous bugs and issues:

    #!/usr/bin/env perl use strict; use warnings; use IPC::System::Simple qw( systemx ); sub usage { print(STDERR "$_[0]\n") if @_; print(STDERR "usage: $0 user muser [ user muser [ ... ] ]\n"); exit(1); } sub get_user_home { return (getpwnam($_[0]))[7]; } usage("Wrong number of args\n") if @ARGV % 2 != 0; my $success = 1; while (@ARGV) { my $user = pop; my $muser = pop; my $home = get_user_home($user); if (!defined($home)) { warn("No such user \"$user\"\n"); $success = 0; next; } if (-d $home) { warn("User \"$user\" already has a home directory\n"); $success = 0; next; } my $mhome = get_user_home($muser); if (!defined($mhome)) { warn("No such user \"$muser\"\n"); $success = 0; next; } if (!eval { systemx(mkdir => ( $home )); systemx(chown => ( "$user:users", $home )); systemx(ln => ( '-s', "$mhome/.profile", "$home/.profile" )); 1 }) { warn($@); $success = 0; next; } } exit($success ? 0 : 1);
Re: switching from <> to $ARGV
by ikegami (Patriarch) on May 20, 2010 at 02:05 UTC
    Change
    LOOP:while (<>) { chomp; ($user, $muser) = map lc, split;
    to
    LOOP:while (@ARGV) { chomp; my $user = pop; my $muser = pop;