By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,847 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,847 IT Pros & Developers. It's quick & easy.

Perl: invoke value

P: 9
hi everyone

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

for example
Expand|Select|Wrap|Line Numbers
  1. getopt('u);
  2. if (opt_u)
  3. {
  4. print"enter something:\n";
  5. $input=<STDIN>;
  6. }
  7.  
  8. getopt('a');
  9. if(opt_a)
  10. {
  11. #here, how can i use the value $input from getopt('u')
  12. }
thank you for any helping
Mar 28 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
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:
Expand|Select|Wrap|Line Numbers
  1. use Getopt::Std;
  2. use strict;
  3.  
  4. my $input;
  5. our($opt_u, $opt_a);
  6.  
  7. getopt();
  8.  
  9. if ($opt_u)
  10. {
  11. print"enter your name:\n";
  12.  $input=<STDIN>;
  13. }
  14.  
  15. if($opt_a)
  16. {
  17. print "Hi\n$input";
  18.  
  19. }
  20.  
  21.  
you can run this script as:
Expand|Select|Wrap|Line Numbers
  1. perl myscript.pl -ua
  2.  
Mar 28 '08 #2

P: 9
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.
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use Getopt::Std; 
  3. getopt('a:u:');
  4.  
  5. # First command switch, adding a user.
  6. if ($opt_a)
  7.     {
  8.     print "Adding user ", $opt_a, "\n";
  9.     password_creation();
  10.         if ($pass1 eq $pass2) {            print "\nSuccess. Adding user to database.\n";
  11.        `useradd $opt_a`;
  12.         } else {
  13.             print "\nPasswords didn't match.\n";
  14.             exit;
  15.         }
  16.  
  17.     sub password_creation {
  18.         print "Enter password:";
  19.         system "stty -echo"; 
  20.         $pass1 = <>;
  21.       system"stty echo";
  22.         print "\n", "Re-enter password:";
  23. system"stty -echo";        
  24. $pass2 = <>;
  25.         system "stty echo";         }
  26.     }
  27.  
  28. if (opt_u)
  29.     {
  30. print "Enter the current password:";
  31.         system "stty -echo"; 
  32.         $pass3 = <>;
  33.       system"stty echo";
##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.
Mar 28 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.