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: Are estimates of regression coefficients uncorrelated?
Answer: This is an important consideration in designing experiments, where it can be desirable to have no (or very little) correlation among the estimates $\hat a$ and $\hat b$.  Such lack of correlation can be achieved by controlling the values of the $X_i$.



To analyze the effects of the $X_i$ on the estimates, the values $(1,X_i)$ (which are row vectors of length $2$) are assembled vertically into a matrix $X$, the design matrix, having as many rows as there are data and (obviously) two columns.  The corresponding $Y_i$ are assembled into one long (column) vector $y$.  In these terms, writing $\beta = (a,b)^\prime$ for the assembled coefficients, the model is

$$\mathbb{E}(Y) = X \cdot \beta$$

The $Y_i$ are (usually) assumed to be independent random variables whose variances are a constant $\sigma^2$ for some unknown $\sigma \gt 0$.  The dependent observations $y$ are taken to be one realization of the vector-valued random variable $Y$.

The OLS solution is

$$\hat\beta = \left(X^\prime X\right)^{-1} X^\prime y,$$

assuming this matrix inverse exists.  Thus, using basic properties of matrix multiplication and covariance,

$$\text{Cov}(\hat\beta) = \text{Cov}\left(\left(X^\prime X\right)^{-1} X^\prime Y\right) = \left(\left(X^\prime X\right)^{-1} X^\prime\sigma^2   
X \left( X^\prime X \right)^{-1\prime}  \right) = \sigma^2 \left(X^\prime X\right)^{-1}.  $$

The matrix $\left(X^\prime X\right)^{-1}$ has just two rows and two columns, each corresponding to the model parameters $(a,b)$.  The correlation of $\hat a$ with $\hat b$ is proportional to the off-diagonal elements, which are just the dot products of the two columns of $X$.  Since one of the columns is all $1$s, whose dot product with the other column (containing the of $X_i$) is their sum, we find


  $\hat a$ and $\hat b$ are uncorrelated if and only the sum (or equivalently the mean) of the $X_i$ is zero.


This condition frequently is achieved by recentering the $X_i$ (by subtracting their mean from each).  Although this will not alter the estimated slope $\hat b$, it does change the estimated intercept $\hat a$.  Whether or not that is important depends on the application.



All this analysis applies to multiple regression: the design matrix will have $p+1$ columns for $p$ independent variables (remember, an additional column consists of $1$s) and $\beta$ will be a vector of length $p+1$, but otherwise everything goes through exactly as before.  In conventional language, two columns of $X$ are called orthogonal when their dot product is zero.  Thus,


  Two multiple regression coefficient estimates $\hat\beta_i$ and $\hat\beta_j$ are uncorrelated if and only if the corresponding columns of the design matrix are orthogonal.


Many standard experimental designs consist of choosing values of the independent variables to make the columns orthogonal.  This "separates" the resulting estimates by guaranteeing--before any data are ever collected!--that the estimates will be uncorrelated.  (When the responses have Normal distributions this implies the estimates will be independent, which greatly simplifies their interpretation.)