Perl: invoke value

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cecil1984
    New Member
    • Mar 2008
    • 9

    Perl: invoke value

    hi everyone

    could everyone tell me how can i invoke value from getopt('u') to getopt('a')?

    for example
    [CODE=perl]
    getopt('u);
    if (opt_u)
    {
    print"enter something:\n";
    $input=<STDIN>;
    }

    getopt('a');
    if(opt_a)
    {
    #here, how can i use the value $input from getopt('u')
    }[/CODE]

    thank you for any helping
    Last edited by eWish; Mar 28 '08, 01:35 PM. Reason: Please use code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    Originally posted by Cecil1984
    hi everyone

    could everyone tell me how can i invoke value from getopt('u') to getopt('a')?

    for example

    getopt('u);
    if (opt_u)
    {
    print"enter something:\n";
    $input=<STDIN>;
    }

    getopt('a');
    if(opt_a)
    {
    #here, how can i use the value $input from getopt('u')
    }

    thank you for any helping
    You need to declare that variable as local to entire script.Also, using barewords 'opt_u' and 'opt_a' is not the right way of using switches. Consider this example:
    [CODE=perl]
    use Getopt::Std;
    use strict;

    my $input;
    our($opt_u, $opt_a);

    getopt();

    if ($opt_u)
    {
    print"enter your name:\n";
    $input=<STDIN>;
    }

    if($opt_a)
    {
    print "Hi\n$input ";

    }

    [/CODE]

    you can run this script as:
    Code:
    perl myscript.pl -ua

    Comment

    • Cecil1984
      New Member
      • Mar 2008
      • 9

      #3
      thanks your reply!

      actually, I need add new user into unix by using -a switch, and after that when you update user by using -u switch, how can i check the current password when i update the user password.
      [CODE=perl]#!/usr/bin/perl
      use Getopt::Std;
      getopt('a:u:');

      # First command switch, adding a user.
      if ($opt_a)
      {
      print "Adding user ", $opt_a, "\n";
      password_creati on();
      if ($pass1 eq $pass2) { print "\nSuccess. Adding user to database.\n";
      `useradd $opt_a`;
      } else {
      print "\nPassword s didn't match.\n";
      exit;
      }

      sub password_creati on {
      print "Enter password:";
      system "stty -echo";
      $pass1 = <>;
      system"stty echo";
      print "\n", "Re-enter password:";
      system"stty -echo";
      $pass2 = <>;
      system "stty echo"; }
      }

      if (opt_u)
      {
      print "Enter the current password:";
      system "stty -echo";
      $pass3 = <>;
      system"stty echo";[/CODE]

      ##problem is here, how can i compare the current input password and the password which is saved in the system. i thought i can use the password which was inputed when run -a switch, but how the admin just update from -u switch directly. i think i need write script to read the password which is saved in the system first, and then compare with the current input password. thank for any helping.
      Last edited by eWish; Mar 28 '08, 07:59 PM. Reason: Please use code tags

      Comment

      Working...