简体   繁体   中英

Bash Script to Create Usernames using AWK

I am writing a bash script that takes information from a text file called "studentlist.txt" and that text file gets passed to the script through a positional parameter. The text file contains firstname, lastname, and ID number. Using that information, I have to create a username and the username is a combination of First Name Initial, Full Last Name, and Last Four digits of the ID number. Afterwards I have to use the usernames created to delete users from the OS. So far, this what I coded thus far.

#! /bin/bash

studentlist=$1 ### Storing the Positional Parameter into the variable called studentlist

if [ $# != 0 ] ### If a positional parameter does exist, then do the following.
        then
                initial=`cat ${studentlist} | awk {'print $1'} | cut -c1`; ### Print the First Field in Studentlist, which is First Name for us. And we only care about the initial.
                lastname=`cat ${studentlist} | awk {'print $2'}`; ### Print the Entire Last Name.
                id=`cat ${studentlist} | awk {'print $3'} | grep -o '....$'`; ### Print only the Last Four Digits of the Student ID.
                username="$initial$lastname$id" ### Concatenate all the variables to create the username.
fi

echo $username ### Print to the Screen all usernames created.

When I run the script, the following shows up on the terminal:

Welcome to the script Remove_Accounts
This script uses utilizes GetOpts, for more information on the script use the flag -h
J O L S K C B EDoe Raborn Gillan Bisram Escudero Espinal Ghamandi Zelma5678 6789 7891 8912 9123 1234 2345 3456

Which isn't what I want, I only want the full username to show up for which user. For example, the usernames should be showing up as the following:

JDoe5678

ORaborn6789

LGillan7891

SBisram8912

KEscudero9123

CEspinal1234

BGhamandi2345

EZelma3456

The text file "studentlist.txt", contains the following information:

John Doe 12345678

Oswaldo Raborn 23456789

Lesia Gillan 34567891

Sammy Bisram 45678912

Kelvin Escudero 56789123

Cecile Espinal 67891234

Boris Ghamandi 78912345

Evia Zelma 89123456

Any help would be greatly appreciated. I have been at this for a long but I can't seem to get it to work. Thank you!

It is not needed to create a variable for each initial , lastname and id . You can just get what you want from one line as:

allusernames=`cat ${studentlist} | awk {'print substr($1,1,1) $2 substr($3,length($3)-3,length($3))'}`

Thus the final script would be,

studentlist=$1 ### Storing the Positional Parameter into the variable called studentlist

if [ $# != 0 ] ### If a positional parameter does exist, then do the following.
        then
    allusernames=`cat ${studentlist} | awk {'print substr($1,1,1) $2 substr($3,length($3)-3,length($3))'}` #Create username as needed from each line

    #Print each username
    for username in $allusernames
    do
        echo $username
    done

fi

Running this would get,

JDoe5678
ORaborn6789
LGillan7891
SBisram8912
KEscudero9123
CEspinal1234
BGhamandi2345
EZelma3456

I hope this is what you wanted.

You need to read the file line by line to get the required output. Here is my code

#! /bin/bash
studentlist=$1 ### Storing the Positional Parameter into the variable called studentlist

if [ $# != 0 ] ### If a positional parameter does exist, then do the following.
                then
                while IFS= read -r students
                do
                initial=`echo ${students} | awk {'print $1'} | cut -c1`; ### Print the First Field in Studentlist, which is First Name for us. And we only care about the initial.
                lastname=`echo ${students} | awk {'print $2'}`; ### Print the Entire Last Name.
                id=`echo ${students} | awk {'print $3'} | grep -o '....$'`; ### Print only the Last Four Digits of the Student ID.
                username="${initial}${lastname}${id}" ### Concatenate all the variables to create the username.
                echo $username ### Print to the Screen all usernames created
                done < $studentlist
fi

I got output as:

JDoe5678
ORaborn6789
LGillan7891
SBisram8912
KEscudero9123
CEspinal1234
BGhamandi2345
EZelma3456

I hope this is what you looking for.

Is there any need to involve a shell script at all? awk alone can handle generating and outputting the combined names without any help from the shell. In your case:

$ awk 'NF==3{printf "%s%s%s\n", substr($1,1,1), $2, substr($3,5)}' studentlist.txt
JDoe5678
ORaborn6789
LGillan7891
SBisram8912
KEscudero9123
CEspinal1234
BGhamandi2345
EZelma3456

It does so without invoking any other subshell. If you need this information for further processing in your script, you can read the generated names into an array and have the user ID info available persistently within your script. All that requires is reading the names into an indexed array, eg

arr=($(awk 'NF==3{printf "%s%s%s\n", substr($1,1,1), $2, substr($3,5)}' studentlist.txt))

Where the call to awk was placed in a command-substitution that is then used to populate the elements of the indexed array.

Look things over and let me know if you have any further questions.

here is a working script:

#!/usr/bin/env bash

PATH_STUDENTS_FILE="studentlist.txt"

USERS_LIST=($(awk '{print substr($1,1,1) $2 substr($3,length($3)-3,length($3))}' "${PATH_STUDENTS_FILE}"))

for USER in "${USERS_LIST[@]}"
do
    echo "Delete user account: ${USER}"
    userdel -r "${USER}"
done

The result:

root@ubuntu:# ./script.sh 
Delete user account: JDoe5678
Delete user account: ORaborn6789
Delete user account: LGillan7891
Delete user account: SBisram8912
Delete user account: KEscudero9123
Delete user account: CEspinal1234
Delete user account: BGhamandi2345
Delete user account: EZelma3456

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM