LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 11-29-2010, 06:03 AM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
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.
 
Old 11-29-2010, 07:42 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Bookworm (Fluxbox WM)
Posts: 1,391
Blog Entries: 54

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
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.
 
Old 11-29-2010, 09:14 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
OK, but this is only normal characters, what about '-' '_' or '.' ?
 
Old 11-29-2010, 09:23 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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.
 
Old 11-30-2010, 01:38 AM   #5
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Yes, it helps.

Thank you very much for the help!
 
Old 11-30-2010, 01:51 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
You're welcome
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I match username from username/ ? abefroman Linux - Newbie 2 04-26-2009 09:10 PM
fedora 8 asking username but during installation i dint give any username ?? herevenku Linux - Newbie 2 08-25-2008 04:37 AM
Differance between su <username> and su <username> - guguma Linux - General 3 08-19-2007 01:01 PM
useradd: invalid username username$ engyeow Fedora 5 12-05-2004 04:35 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration