Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

adding usernames and passwords in linux

Status
Not open for further replies.

paublo

ISP
Sep 14, 2006
127
US
hi i dont know much about perl so im hoping someone can help me.

i have a billing system that creates users on a linux mail server with a perl script. the billing system passed 2 values, the username and password to the script.

the script creates the username and places the users's home fir in /home/$username.

what i want to do is have it place the user in /home/1st letter of username/$username. I would like the home dir letter never to be in caps.


the pl script im using has these lines

#!/usr/bin/perl

open(INFILE, "@ARGV[0]");
$infile = <INFILE>;
($username,$password) = split(/\|/, $infile);
close(INFILE);

@calladduser = ("/usr/sbin/adduser $username -g 503 -d /home/$letter/$username -s /sbin/nologin -p $password","\n");
system("@calladduser");

@cryptpasswd = ("echo $password | passwd $username --stdin");
system("@cryptpasswd");

what can i do on this script that will place the 1st letter of the username in $letter.

Thanks for all your help.
 
thank you for your response i will try that but i also found a script that i forgot about using
my ($firstletter) = $username =~ /^(.)/;
which works.

thanks for the reply.

p
 
paublo said:
what i want to do is have it place the user in /home/1st letter of username/$username. I would like the home dir letter never to be in caps.
You might want to look at the lc function for that.
 
my ($firstletter) = $username =~ /^(.)/;

It works and its probably the least efficient way to do something like that. The most efficient way to get the first character from a sting into a variable is unpack():

Code:
my $first = unpack ("A1", $string);

Combine that with lc() and you have what you need in a fast and efficient manner:

Code:
my $first = lc(unpack ("A1", $string));

regular expressions are for searching/matching patterns, they are powerful but less efficient tools when all you need to do is some simple string processing.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top