0

Writing script to create user accounts. Script is written to echo the useradd command (so I can make sure it will work as expected).

I have a file newemploy.txt with Employee Names (first and last, space-separated). I need a variable $FULLNAME that will read from newemploy.txt.

Here is my script:

#!/bin/bash

# This creates a file unames.txt containing usernames created from employees' full name

awk '!(NR%2){print substr($1,1,1)p}{p=substr($2,1,4)}' txtfiles/newemploy.txt | tr [:upper:] [:lower:] > txtfiles/unames.txt

GROUPID=500
USERID=500

for UNAME in `cat txtfiles/unames.txt`

do

echo "useradd -u$USERID -g$GROUPID -c "$FULLNAME" -d /home/$UNAME -m -s /bin/bash $UNAME"

GROUPID=`expr $GROUPID + 1`

USERID=`expr $USERID + 1`

done
0

Try something along these lines:

while read firstname lastname
do
    fullname="$firstname $lastname"
    uname=$(echo ${firstname:0:1}${lastname:0:4} | tr '[:upper:]' '[:lower:]')
    echo $fullname $uname
done  < newemploy.txt
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.