簡體   English   中英

使用 AWK 創建用戶名的 Bash 腳本

[英]Bash Script to Create Usernames using AWK

我正在編寫一個 bash 腳本,它從名為“studentlist.txt”的文本文件中獲取信息,並且該文本文件通過位置參數傳遞給腳本。 文本文件包含名字、姓氏和 ID 號。 使用這些信息,我必須創建一個用戶名,用戶名是名字首字母、全名和 ID 號碼的最后四位數字的組合。 之后我必須使用創建的用戶名從操作系統中刪除用戶。 到目前為止,這是我迄今為止編碼的內容。

#! /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.

當我運行腳本時,終端上會顯示以下內容:

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

這不是我想要的,我只想顯示哪個用戶的完整用戶名。 例如,用戶名應顯示如下:

JDoe5678

ORaborn6789

LGillan7891

SBisram8912

KEscudero9123

CEspinal1234

BGhamandi2345

EZelma3456

文本文件“studentlist.txt”,包含以下信息:

約翰·多伊 12345678

奧斯瓦爾多·拉伯恩 23456789

萊西亞·吉蘭 34567891

薩米比斯拉姆 45678912

開爾文埃斯庫德羅 56789123

塞西爾·埃斯皮納爾 67891234

鮑里斯·加曼迪 78912345

埃維亞澤爾瑪 89123456

任何幫助將不勝感激。 我已經在這方面工作了很長時間,但我似乎無法讓它發揮作用。 謝謝!

不需要為每個initiallastnameid創建一個變量。 您可以從一行中獲取您想要的內容:

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

因此,最終的腳本將是,

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

運行這個會得到,

JDoe5678
ORaborn6789
LGillan7891
SBisram8912
KEscudero9123
CEspinal1234
BGhamandi2345
EZelma3456

我希望這就是你想要的。

您需要逐行讀取文件以獲得所需的輸出。 這是我的代碼

#! /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

我得到的輸出為:

JDoe5678
ORaborn6789
LGillan7891
SBisram8912
KEscudero9123
CEspinal1234
BGhamandi2345
EZelma3456

我希望這就是你要找的。

有沒有必要涉及一個shell腳本? awk可以單獨處理生成和輸出組合名稱,而無需 shell 的任何幫助。 在你的情況下:

$ 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

它在不調用任何其他子shell的情況下這樣做。 如果您需要此信息在您的腳本中進一步處理,您可以將生成的名稱讀入一個數組,並在您的腳本中永久提供用戶 ID 信息。 所需要的只是將名稱讀入索引數組,例如

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

awk的調用被放置在命令替換中,然后用於填充索引數組的元素。

仔細檢查一下,如果您有任何其他問題,請告訴我。

這是一個工作腳本:

#!/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

結果:

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