【发布时间】: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