LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   Strip that checks if added username can be a username (https://www.linuxquestions.org/questions/linux-server-73/strip-that-checks-if-added-username-can-be-a-username-847189/)

robertjinx 11-29-2010 06:03 AM

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.

neonsignal 11-29-2010 07:42 AM

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).

robertjinx 11-29-2010 09:14 AM

OK, but this is only normal characters, what about '-' '_' or '.' ?

druuna 11-29-2010 09:23 AM

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.

robertjinx 11-30-2010 01:38 AM

Yes, it helps.

Thank you very much for the help!

druuna 11-30-2010 01:51 AM

You're welcome :)


All times are GMT -5. The time now is 04:38 AM.