Ric Posted January 28, 2004 Report Share Posted January 28, 2004 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. Thanks for any help. Ric Quote Link to comment Share on other sites More sharing options...
aru Posted January 28, 2004 Report Share Posted January 28, 2004 (edited) 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 January 28, 2004 by aru Quote Link to comment Share on other sites More sharing options...
Ric Posted January 28, 2004 Author Report Share Posted January 28, 2004 Thanks for the quick response, that is exactly what i amm looking for . Cheers Ric Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.