Jump to content

Can Someone Help With a Script to..


Recommended Posts

Can some one help me with a script that can read a list of full names, then strip all but the first 3 letters of the first name and join it to the the surname to make a username.

 

ie Full Name richard smith --- becomes ricsmith

 

unless there is already a user called ricsmith in which case it would make richsmith .

 

then run the useradd command :

 

useradd -d /home/ricsmith -g some_group -p (get a password from some other file)

 

then out print out the fullname, username and password to yet another file .

 

 

Dose any of that even make sense. :wall:

 

Thanks for any help.

 

Ric

Link to comment
Share on other sites

some hints:

 

Can some one help me with a script that can read a list of full names, then strip all but the first 3 letters of the first name and join it to the the surname to make a username.

 

ie Full Name richard smith --- becomes ricsmith

 

This is one way (of hudreds):

 

~$ echo 'richard smith' | sed -n "s/^\(.\{3\}\).[^ ]*[ ]\(.[^ ]*\)/\1\2/p"

 

 

unless there is already a user called ricsmith in which case it would make richsmith .

 

Just check the generated string against a list of users and if there is a match run again the sed command changing 3 with 4; for example (not real code):

 

~$ cat > script
#! /bin/bash
init_name_chars=3

cat list_of_full_names | while read FULLNAME; do
     name_chars=$init_name_chars
     while :; do
               NICKNAME=$(echo "$FULLNAME" | sed -n "s/^\(.\{${name_chars}\}\).[^ ]*[ ]\(.[^ ]*\)/\1\2/p")
               if grep -q $NICKNAME list_of_users 2>/dev/null; then
                             name_chars=$((name_chars + 1))
               else break
               fi
     done
     echo $NICKNAME | tee -a list_of_users
done

 

Probably the above code will work, you'll only will need to fit the ideas exposed in the above code with what you want to do

 

 

then run the useradd command :

 

useradd -d /home/ricsmith -g some_group  -p  (get a password from some other file)

 

unless you know how to encript the password passed through -p, you'll need to use the command passwd in addition to useradd; for example:

~# useradd ricsmith && echo 'thepassword' | passwd --stdin ricsmith

 

 

then out print out the fullname, username and password to yet another file .

 

Just store the desired values in variables and echo them into a file on each pass of the loop

 

 

HTH

Edited by aru
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...