variable:OFFSET:LENGTH

One of the things we wanted to do earlier is to automatically create usernames from the users first name and their surname.

For example, in the file to create users, I have a first name, a surname and the users primary group.

<FirstName> <Surname> <PrimaryGroup>

Hamish 			Whittal 	users
            

I would like to combine the surname and the first three letters of the firstname to create the users username - automatically!

We can use another of these parameter substitution constructs to achieve this:

${variable:OFFSET:LENGTH}
            

where the OFFSET begins at 0 and LENGTH is the number of characters to keep in the total length of the user name.

Assuming I have read the above values into the variables:

FIRSTNAME SURNAME and PGROUP
            

As a result, we could use the following code to resolve our issue:

1.Chop the FIRSTNAME to the first 3 characters using our new construct:

SHORT_FIRST=${FIRSTNAME:0:3}
            

which would leave us with:

SHORT_FIRST=Ham
            

2.Add the SURNAME to the shortened FIRSTNAME:

USERNAME=${SURNAME}${SHORT_FIRST}
            

3.useradd -g ${PGROUP} -c "${FIRSTNAME} ${SURNAME}" -d /home/${USERNAME}

${USERNAME}
            

4.Now set up a script to make that work for every user listed in the userlist.txt file, use a loop construct to ensure that the entire file is read (the file to create users), and BINGO, you have just made your life easier!