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
1
  • 1
    The question is? Commented Jun 2, 2016 at 5:55

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .