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

Can any one help me please

P: 2
I wrote the program and its not giving me correct answer can any one help me with that please and specify my mistake please it will be highly appreciable...
The error arrives from option 'a' it asks for user name, check in the system but does not return the correct answer please help me with it.

or if you have better way of doing it would you please mind to tell me..

thanks..

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. #use Getopt::Std;
  4.  
  5. print "\nWelcome to the New Era!! Assignment 1!! \n\nFor help to run program press h !!\n\n ";
  6.  
  7. #
  8. # Take input from user
  9. #
  10. print "\nEnter the Option --> : ";
  11. chomp ($option = <>);
  12.  
  13. while($option ne "q")
  14. {
  15. #
  16. # Adding user option
  17. #
  18.   if($option eq "a")
  19.   {
  20.     print "\n\n--- Add User Menu --- \n\n";
  21.     print "\nEnter User Name --> : ";
  22.     chomp ($userName = <>);
  23. #
  24. #Checking for the user if its exist in the UNIX
  25. #
  26.     $chkrs = 'grep -c -w $username /etc/passwd'; # grep : search files for lines, match given pattern
  27. print "\n User $chkrs \n";
  28.     if($chkrs == 0 )  # 0 means user does not exists
  29.     {
  30.     # the new user will create in UNIX DATABASE
  31. #
  32. #  execute the Linux/Unix command in b/w ' '
  33. #
  34.     `useradd $userName -d /home/$userName`; #useradd(create new user account -d(widout option))
  35.     `chmod 700 /home/$userName`; # change the access permission of the files
  36.     `chown -hR $userName /home/$userName`; # change ownership of user and group files
  37.     `passwd $userName`; #modify/creat a user password in UNIX database
  38.     `echo $mypass | smbpasswd -a $userName -s`;
  39.     `echo $mypass | htpasswd -bcs /home/$userName/.htpasswd $userName -s`;
  40.          print "\nEnter the Option --> : ";
  41.          chomp ($option = <>);
  42.     }
  43. #
  44. #it will run if user already exists!
  45. #
  46.     else
  47.     {
  48.     print"\n User already exists! Try again .. \n\n";
  49.         print "\nEnter the Option --> : ";
  50.         chomp ($option = <>);
  51.     }
  52.  
  53.   }
  54. #
  55. # Updating user option
  56. #
  57.   elsif($option eq "u")
  58.   {
  59.     print "\n\n--- Updating User Menu --- \n\n";
  60.     print "\nEnter the Option --> : ";
  61.     chomp ($option = <>);
  62.   }
  63. #
  64. # Deleting user option
  65. #
  66.   elsif($option eq "d")
  67.   {
  68.     print "\n\n--- Deleting User Menu --- \n\n";
  69.     print "\nEnter the Option --> : ";
  70.     chomp ($option = <>);
  71.   }
  72. #
  73. # Help Menu Option
  74. #
  75.   elsif($option eq "h")
  76.   {
  77.     print "\n\n--- Help Menu --- \n\nExit program --> press q\n\nAdding user --> press a\n\nUpdating Password --> press u\n\nDelete user --> press d\n\n";
  78.     print "\nEnter the Option --> : ";
  79.     chomp ($option = <>);
  80.   }
  81. #
  82. # If user press wrong option
  83. #
  84.   else 
  85.   {
  86.     print "\n\nYou select the wrong Option \n\nFor help press h \n\n";
  87.     print "\nEnter your option --> : ";
  88.     chomp ($option = <>);
  89.   }
  90.  
  91. }
  92.  
  93. #
  94. # option q logic
  95. #
  96. until($option ne "q")
  97. {
  98.   print "\nThe program is terminating. . . . . .\n\n";
  99.   exit;
  100. }
Mar 23 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
Your code is a perfect example of why you should use the "strict" pragma:

use strict;

chomp ($userName = <>);
Checking for the user if its exist in the UNIX
$chkrs = 'grep -c -w $username /etc/passwd';

See the problem? "strict" would have caught that typo right away.
Mar 24 '08 #2

P: 2
thanks mate..

i change it...
but it does not work

can you please tell me about htpasswd coz its not running at my side

regards
Mar 24 '08 #3

nithinpes
Expert 100+
P: 410
thanks mate..

i change it...
but it does not work

can you please tell me about htpasswd coz its not running at my side

regards
You have used single quotes for passing command to terminal in this line of your initial script:
Expand|Select|Wrap|Line Numbers
  1. $chkrs = 'grep -c -w $username /etc/passwd';
  2.  
Change that to reverse quotes:
Expand|Select|Wrap|Line Numbers
  1. $chkrs = `grep -c -w $username /etc/passwd`;
  2.  
You have used reverse quotes properly for passing commands in remaining part of your script. Have you rectified this and tried?
Mar 24 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
You have used single quotes for passing command to terminal in this line of your initial script:
Expand|Select|Wrap|Line Numbers
  1. $chkrs = 'grep -c -w $username /etc/passwd';
  2.  
Change that to reverse quotes:
Expand|Select|Wrap|Line Numbers
  1. $chkrs = `grep -c -w $username /etc/passwd`;
  2.  
You have used reverse quotes properly for passing commands in remaining part of your script. Have you rectified this and tried?

Good catch, I entirely missed the single-quotes problem.
Mar 24 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
thanks mate..

i change it...
but it does not work

can you please tell me about htpasswd coz its not running at my side

regards

You need to ask questions that can be answered. You may as well ask:

Can you please tell me about automobiles coz mine is not running.

How can you answer a question like that?
Mar 24 '08 #6

Post your reply

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