Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
hi, im just simply trying out writing scripts for fun, I have done a simple adduser script in perl, but now i want to modify a bit.
Here is my script
#!/usr/bin/perl
print "pls enter name\n";
$name = <STDIN>;
system("useradd $name");
if let say when i execute the script and the name i choose is "bond", a user is created succesfully
But when i execute the script again and the name i input is again "bond", it will display useradd: user bond exist.
What I would like to do is, when a same name is entered, I want to display my own message like "pls try again", how to check the /etc/passwd file and then display the message i prefer.
hi, im just simply trying out writing scripts for fun, I have done a simple adduser script in perl, but now i want to modify a bit.
Here is my script
#!/usr/bin/perl
print "pls enter name\n";
$name = <STDIN>;
system("useradd $name");
if let say when i execute the script and the name i choose is "bond", a user is created succesfully
But when i execute the script again and the name i input is again "bond", it will display useradd: user bond exist.
What I would like to do is, when a same name is entered, I want to display my own message like "pls try again", how to check the /etc/passwd file and then display the message i prefer.
pls help
Thank you
john
First of all, to add users you need to be running this as root, or the useradd command thru sudo or similar, what makes the use of a variable directly in the system function really dangerous.
Users running this script could easily get root access.
Or users just trying to input a "cool" name with lot of unsupported chars will get an error. You need to sanitize and validate the variable before passing it to the system function.
if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
}
************************************
when i execute the script, and i insert any names, it will does not exist on the system. I have also insert a username that is already in /etc/passwd itself, stil the same result
Last edited by john83reuben; 03-11-2008 at 07:35 AM.
#!/usr/bin/perl
print "pls enter name\n";
$username = <STDIN>;
system("useradd $username");
if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
}
You should really place the code within code tags as suggested.
You should also consider the problem with root that was suggested.
edit: the code tags are at the top of the reply or edit window
Last edited by onebuck; 03-11-2008 at 08:04 AM.
Reason: code tags
Not sure if your still messing with this but here is what I what I used:
Code:
#!/usr/bin/perl
print "pls enter name\n";
$username = <STDIN> ;
chop($username);
if (defined(getpwnam($username)))
{
print "$username exists on the system\n";
}
else
{
print "$username does not exist on the system\n";
print "Creating $username\n";
system("useradd $username");
system("passwd $username");
system("chage -d 0 $username");
}
The chop() takes the '/n' off of the end of the <STDIN> which is why the
script was always returning "$username does not exist on the system".
Also I added the "passwd" command and "chage -d 0" which will force the newly created user to change their password at 1st login.
I ALWAYS use those strictures in every Perl prog, even very short ones. Its amazing how often it turns out to be a good idea.
Otherwise, you write a quickie, without strictures,that 'seems' to work, then later, you borrow that code & put it in a 'real' prog and all of a sudden you either get mysterious errors (if you haven't got them in the real prog), or you immediately get error/warning msgs.
I test new progs by doing
perl -wc newcode.pl
does a test compile without execution. If you have the strictures in there, it saves you from yourself.
Maybe its because I did a lot yrs using C, but I'm a bit anal about clean code....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.