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

Hi All, i need to be able to run a command like this myperlscipt.pl username=bob password=abc123 which will take the data from username and password and place it in my script below, as variables $username and $password system ("adduser $username -p $password"); any help will be much appreciated

Replies are listed 'Best First'.
Re: Taking Input And Running In Script
by tadman (Prior) on Jul 31, 2002 at 18:41 UTC
    You might consider using something like Getopt::Long:
    #!/usr/bin/perl -w use strict; use Getopt::Long; my ($username, $password); my $result = GetOptions("username=s" => \$username, "password=s" => \$password);
    Now you can do this:
    % myscript --username=foo --password=bar
    As a note, putting the password in the command line on a system which is not 100% your own is a really bad idea since this password will show up in the process listing table. Some programs actually mangle the $0 variable to hide this:
    $0 =~ s/password=\S+/password=xxx/;
    But there still is a narrow window of opportunity where they might see it.
Re: Taking Input And Running In Script
by simeon2000 (Monk) on Jul 31, 2002 at 18:54 UTC
    If you're in unix, and you don't want your PASSWORD to appear on the command line, you can set it in environment variables like so:

    $ USERNAME=jim PASSWORD=spekkio perl script.pl

    Then the code could:

    print $ENV{USERNAME}, $ENV{PASSWORD}

    However, this approach also has some pitfalls, and you must be VERY careful with it... but at least it's better than having your password in the process table :P

    --
    perl -e "print qq/just another perl hacker who doesn't grok japh\n/"

      cheers guys for that.

      that worked great, but if you don't mind, cna i ask you one more question? This is the script that i am using below:

      #!/usr/bin/perl -w use strict; use Getopt::Long; my ($username, $password, $domain); my $result = GetOptions("username=s" => \$username, "password=s" => \$password, "domain=s => \$domain"); system ("adduser $username -g 100 -s /bin/false -d /home/$username -p +$password"); + open (VFILE, ">> /etc/postfix/virtual"); + + print VFILE "$username\@ap3k.com\t$username\n"; close (VFILE); system ("postmap /etc/postfix/virtual");

      when the command adduser is run, if it returns adduser: user foo already exists

      i want it to break out of running the rest of the script and maybe just return an error

      James

      Edited: ~Wed Jul 31 23:07:40 2002 (GMT) by footpad: Added <code> and other HTML formatting tags.

        first, please use code tags. As for your question of breaking out, check the return value of system ($?). Most unix commands return 0 upon success and something else upon failure (not quite sure what the failure codes are for adduser but I get a 9 when you try to add an all ready existing user).

        #!/usr/bin/perl -w use strict; use Getopt::Long; my ($username, $password, $domain); my $result = GetOptions("username=s" => \$username, "password=s" => \$password, "domain=s" => \$domain ); system("/usr/sbin/adduser $username -g 100 -s /bin/false -d /home/$use +rname -p $password"); if( $? >> 8 == 0 ) { # success open (VFILE, ">> virtual"); # you should check for failure here print VFILE "$username\@ap3k.com\t$username\n"; close (VFILE); # Do you need to check this? system ("postmap /etc/postfix/virtual"); } else { print STDERR "bailing with ret code of ", $? >> 8, "\n"; }

        see system on interpretting return codes (that shifty bit stuff).

        -derby

        Ouch ...Please use code tags next time. On your question( at least what I think you question is ;) Try just using adding or die "whatever you want the error to be $!\n" to the system line -Aseidas


        UPDATE:disregard my method for the error trapping. Please use Derby's method mine is very simplistic and in reading your post again will not really do what you want i.e; exit on that specific error but please do use code tags