This script takes a .txt file with four columns- which contains LastName FirstName MiddleInitial Group-as an argument and needs to create a unique username and password for each person; and then assign each user the appropriate directory depending on their group: i.e. If "John Doe" is in "mgmt" group, and his username is jdoe1234, then his directory would be /home/mgmt/jdoe1234. It should then generate a .txt file which contains the following columns- LastName FirstName UID(userid) Password- .

I have the following:

#!/bin/bash
IFS=$'\n';
for i in `cat $1`;
do
    last=`echo $i|cut -f 1 -d ' '`;
    first=`echo $i|cut -f 2 -d ' '`;
    middle=`echo $i|cut -f 3 -d ' '`;
    groups=`echo $i|cut -f 4 -d ' '`;
    r=$(( $RANDOM % 10 ));
    s=$(( $RANDOM % 10 ));
    y=$(( $RANDOM % 10 ));
    username=`echo $first| head -c 1 &amp;&amp; echo $last| head -c 3 &amp;&amp; echo $r$s$y`
    echo $username
done
#check if group exists, if not then create one
for group in ${groups[*]}
do
    grep -q "^$group" /etc/group ; let x=$?
    if [ $x -eq 1 ]
    then
            groupadd "$group"
    fi
done

#try to add user to correct group
x=0
created=0
for user in ${username[*]}
do
    useradd -n -g "{groups[$x]}" -m $user 2&gt; /dev/null
done


I want the username to contain: 1st letter of firstName, first 3 letters of the lastName, the middle initial, and then 3 randomly generated numbers. So not exactly the same as the above example with John Doe but similar. It can't be any more than 8 characters. I'm not sure if I'm creating the usernames properly.

Of course, I'm having trouble with the password too; not sure if it needs to be created alongside the username or after.

After the first 'for loop' I first try to add a group if it doesn't already exist, and then I make an attempt at putting the usernames into the correct groups. I got the syntax off a Youtube video but he was working with it as arrays and I'm not sure if I'm doing that or not.

If it helps, let's say the .txt file contains:

doe john a mgmt
lee amy f temp
smith tracy s empl


If you have time, any help at all would be appreciated. Thank you.


Question: "wouldn't" vs. "won't" & "couldn't" vs "can't"
Answer: This serves as a complement to the MMJZ's answer.

Without contexts, the first sentence in the second group could be paraphrased in the following ways:


  
  We all know that machines couldn't work without energy. (It is impossible that machines work without energy.)
  We're running out of fossil fuels. Machines couldn't work without energy. (Machines would not possibly work without energy.)
  In fourteenth century people didn't know machines couldn't work without energy. (Machines weren't able to work without energy.)