简体   繁体   English

使用 AWK 创建用户名的 Bash 脚本

[英]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.我正在编写一个 bash 脚本,它从名为“studentlist.txt”的文本文件中获取信息,并且该文本文件通过位置参数传递给脚本。 The text file contains firstname, lastname, and ID number.文本文件包含名字、姓氏和 ID 号。 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.使用这些信息,我必须创建一个用户名,用户名是名字首字母、全名和 ID 号码的最后四位数字的组合。 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:文本文件“studentlist.txt”,包含以下信息:

John Doe 12345678约翰·多伊 12345678

Oswaldo Raborn 23456789奥斯瓦尔多·拉伯恩 23456789

Lesia Gillan 34567891莱西亚·吉兰 34567891

Sammy Bisram 45678912萨米比斯拉姆 45678912

Kelvin Escudero 56789123开尔文埃斯库德罗 56789123

Cecile Espinal 67891234塞西尔·埃斯皮纳尔 67891234

Boris Ghamandi 78912345鲍里斯·加曼迪 78912345

Evia Zelma 89123456埃维亚泽尔玛 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 .不需要为每个initiallastnameid创建一个变量。 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?有没有必要涉及一个shell脚本? awk alone can handle generating and outputting the combined names without any help from the shell. awk可以单独处理生成和输出组合名称,而无需 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.它在不调用任何其他子shell的情况下这样做。 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.如果您需要此信息在您的脚本中进一步处理,您可以将生成的名称读入一个数组,并在您的脚本中永久提供用户 ID 信息。 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.awk的调用被放置在命令替换中,然后用于填充索引数组的元素。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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