Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

NEED HELP IN LINUX SCRIPTING. NEED TO CREATE A SCRIPT THAT ALLOWS NEW USERS TO B

ID: 3763681 • Letter: N

Question

NEED HELP IN LINUX SCRIPTING. NEED TO CREATE A SCRIPT THAT ALLOWS NEW USERS TO BE ADDED. The script should take no command line arguments, but should read a list of new users to add from a file that is redirected from stdin using the < operator. You must check that you get exactly zero arguments to the script. This input file should contain a series of lines, one for each new user to add. Each line has two fields: the first name and the last name of the new user, separated by a comma. Here is an example file named NEW_USERS

JOHN, DOE

BOBBY, Smith

Janet, Woo

This file specifies three new users to be added. There are two tricky parts about this script. First, you have to build the usernames automatically from the first and last names. Second, you have to set an initial password for each new user. Best practice is to create a random password for each user. Both of these tasks require the use of the "tr" command that is used to translate and/or delete characters from a stream of input:

1. The simplest form of the command is: echo "STRING" | tr SET1 SET2

2. This translates every character in STRING by switching any characters in SET1 to the corresponding character in SET2.

3. The most common usage is to translate upper case characters to lower case (or vice versa). To do that: echo "SomeThing" | tr 'A-Z' 'a-z'

4. The tr 'A-Z' 'a-z' says to translate any A to a, any B to b, any C to c, and so on. The output of the above command would thus be "something" (the S changed to lower case s and the T changed to lower case t). All other characters are left unchanged. So, to generate the username you'll need to:

1. Get the first and last names out of the file individually.

2. Translate both to lower case.

3. Use the bash substring syntax we used previously or use cut to get the first letter of the last name.

4. Concatenate the first name and the first letter of the last name.

5. Assume that the username is not in use (you don't need to check that someone else already has that username for now). Recall that you can use the cut command to split each line up into the first and last name fields (by comma separator). Also, you'll need to remember how to use while loops that read lines from stdin, so that you can repeat this processing for each user in the file.

Explanation / Answer

#!/bin/bash

getFirstCharFromLastName() {

value=`echo $1|tr -d [:space:]|cut -c1`

echo $value

}

generateUserName () {

firstname=$1

charFromLastName=`getFirstCharFromLastName $2`

username=${firstname}${charFromLastName}

echo $username

}

generatePassword (){

pwd=`echo $1|tr a-z [:alnum:]`

echo $pwd

}

if [ $# -gt 0 ]

then

echo "Arguments supplied. Exiting !!"

exit 1

fi

while IFS= read -r line

do

lowerCasedLine=`echo $line|tr A-Z a-z`

firstname=`echo $lowerCasedLine|cut -d"," -f1`

lastname=`echo $lowerCasedLine|cut -d"," -f2`

username=`generateUserName $firstname $lastname`

echo "username is :" $username

password=`generatePassword $username`

echo "password is :" $password

done