【问题标题】:Bash Script to Create Usernames using AWK使用 AWK 创建用户名的 Bash 脚本
【发布时间】:2020-04-03 06:48:38
【问题描述】:

我正在编写一个 bash 脚本,该脚本从名为“studentlist.txt”的文本文件中获取信息,并且该文本文件通过位置参数传递给脚本。文本文件包含名字、姓氏和 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

任何帮助将不胜感激。我已经在这工作了很长时间,但我似乎无法让它发挥作用。谢谢!

【问题讨论】:

  • 感谢您在帖子中分享您的尝试,由于不清楚,请添加您的示例 Input_file。也请使用代码标签将它们包装在您的问题中。
  • initial 包含所有用户的首字母(虽然计算方式非常复杂)。同样,lastname 包含所有用户的姓氏。当然,如果你把$initial$lastname 粘在一起,你就会得到你发布的输出。但是,我不清楚您想在username 中使用什么字符串。
  • @RavinderSingh13 我已经更新了这个问题。现在应该更清楚了。

标签: linux bash shell awk scripting


【解决方案1】:

不需要为每个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

我希望这是你想要的。

【讨论】:

    【解决方案2】:

    您需要逐行读取文件以获得所需的输出。这是我的代码

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

    我希望这就是你要找的。​​p>

    【讨论】:

      【解决方案3】:

      是否需要涉及一个 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 的调用被放置在 command-substitution 中,然后用于填充索引数组的元素。

      查看一下,如果您有任何其他问题,请告诉我。

      【讨论】:

      • 只是为了表明每隔一段时间,即使是盲松鼠也会发现坚果......
      【解决方案4】:

      这是一个工作脚本:

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

      【讨论】:

        猜你喜欢
        • 2020-07-21
        • 2017-11-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-07
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        相关资源
        最近更新 更多