Linux - Server This forum is for the discussion of Linux Software used in a server related context. |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
11-29-2010, 06:03 AM
|
#1
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Rep:
|
Strip that checks if added username can be a username
Hello, does anyone know how to do a small script which checks that a username can be a username.
Im talking about the special charactes and spaces which can't be used when creating a username.
So a user writes: "kevin little" in this case this can't be a username as it contains space.
or
"kevin\little" in this case this can't be a username as it contains special character '\'.
or
"kevin_little" in this case it can be a username as there are only accepted charactes.
If someone knows how to do this please let me know.
Thank you.
|
|
|
11-29-2010, 07:42 AM
|
#2
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
|
You could use bash regular expressions:
Code:
if [[ "$NAME" =~ ^[a-z][-a-z0-9]*$ ]]; then echo 'okay'; fi
(or grep if it needs to be portable to older versions of bash).
Last edited by neonsignal; 11-29-2010 at 07:44 AM.
|
|
|
11-29-2010, 09:14 AM
|
#3
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Original Poster
Rep:
|
OK, but this is only normal characters, what about '-' '_' or '.' ?
|
|
|
11-29-2010, 09:23 AM
|
#4
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
Hi,
Officially this is the legal template for a linux/unix username: [a-z_][a-z0-9_-]*
The only character missing in post #2 is a _ (twice)
^[a-z][-a-z0-9]*$ should be ^[a-z_][-a-z0-9_]*$
Hope this helps.
|
|
|
11-30-2010, 01:38 AM
|
#5
|
Member
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749
Original Poster
Rep:
|
Yes, it helps.
Thank you very much for the help!
|
|
|
11-30-2010, 01:51 AM
|
#6
|
LQ Veteran
Registered: Sep 2003
Posts: 10,532
|
You're welcome 
|
|
|
All times are GMT -5. The time now is 02:25 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|