我使用Linux而且我是新编码的人,我需要项目帮助。
#!/bin/bash
# findName.sh - written by Corey Brown
#
# for use in some project
File="/acct/common/CSCE215-Fall17"
if [[ $1 = "" ]] ; then
echo "usage: `basename $0` [only_one_argument]"
exit 2
fi
# read the file line by line
while read LINE
do
# file format: First, Middle, Last, UserID
# set the index values based on the line format given above
firstNameIndex=0
middleNameIndex=1
lastNameIndex=2
userIDIndex=3
# read the line into an array
IFS=', ' read -r -a lineArray <<< "$LINE"
# if the passed parameter equals the value of the line at userIDIndex
if [[ $1 -eq ${lineArray[$userIDIndex]} ]] ; then
echo ${lineArray[$firstNameIndex]} ${lineArray[$middleNameIndex]} ${lineArray[$lastNameIndex]}
exit 1
fi
done < "$File"
目标是搜索用户ID,并找到与之关联的名称。我正在搜索的文件格式为FirstName,MiddleName,LastName,UserID
。有78行都具有不同的用户ID和名称。这是文件:
Adarius,Macarthy,Adams,adamsam9
Ahmed,Abdulkarim,Hulwe,ahmedh
Ainsley,Grace,McWaters,mcwatera
Alexander,Steven,Hammett,hammeta
Allison,Rose,Rogers,arr2
Andrew,Robert,Tant,atant
Ashton,Nathaniel,Bass,anbass
Austin,Michael,Aguilera,ama4
Austin,Thomas,Blackwell,atb
Ayla,Beth,Nickerson,aylan
Bennett,Steven,Marra,bmarra
Brennan,Sanders,Cain,bscain
Brian,William,Greene,bwgreene
Byron,Javier,Carranza,carranza
Cardi,Stone,Ireland,cireland
Chad,Allen,Kraus,ckraus
Chandni,Bhavesh,Amin,camin
Chase,Michael,Henry,chasemh
Christian,Alexander,Brock,cab11
Christofer,Thomas,Cooper,cooperct
Christopher,Gregory,Masselli,masselli
Cody,James,Rose,cjrose
Colby,Franklin,McHugh,cmchugh
Cole,Peiffer,Kirby,cpkirby
Connor,Niles,Latterell,cnl
Conrad,Joseph,Hetzer,chetzer
Corey,Daniel,Brown,browncd4
Curtis,Allen,Ward,curtisaw
Dajshan,Montrel,Murphy,dajshan
Damion,Ray'shard,Graham,drgraham
Denzel,Malik,Wilson,dmwilson
Derek,,Hebert,dhebert
DeVonte,,Lyles,dlyles
D'mitri,Lamont,Williams,dmitri
Dorothy,Candra,Joyner,dcjoyner
Drew,Graham,Bussey,dgbussey
Edward,A,Perreyclear,perreyce
Emmett,Poindexter,Hatcher,ehatcher
Eric,Wayne,Davies,ewdavies
Frances,Leah,Dickson-Vandervelde,fld1
Hayley,Cy,Lichtenfels,hayleyl
Hendrick,Samuel,McCullar,hsm1
Hunter,Jarrett,Damron,hdamron
Ibrahim,J,Salman,ijsalman
Jahred,Elisha,Danker,jdanker
James,Wade,Curlee,jcurlee
Jared,Edward,Elliott,jarede
John,M,Lee,jml4
Jonathan,,Isaac,ji7
Joseph,Chambers,Martin,jcm5
Kaitlyn,Marie,Ash,kmash
Kristeen,Elizabeth,Ginn,keginn
Kunj,Girish,Naik,kgnaik
Logan,Robert,Orr,lrorr
Luke,Ellison,Whittle,whittlel
Luke,Joseph,Imholz,limholz
Mackenzie,Carter,West,mcw3
Matthew,Robert,Lassiter,lassitm
Matthew,Thomas,O'Neill,mtoneill
Nathaniel,James,Mills,njmills
Nicole,,Dorney,ndorney
Pranav,Raghu,Minasandram,pranavm
Ralph,Tristin,DeFilio,rdefilio
Robert,Brogdon,Gamble,rbgamble
Rohan,,Bhandari,rohanb
Silas,,Steiger,ssteiger
Steven,Edward,Maxwell,sem15
Steven,George,Edwards,stevenge
Suzanne,Terese,Prentice,suzannep
Taylor,Aric,Gordon,tag2
Thomas,Matthew,Sullivan,tms3
Vincent,Cordell,Arceo,varceo
Wade,Christopher,Lewis,wclewis
Wade,Prescott,Morgan,wpmorgan
William,Alexander,Simmons,wsimmons
William,Dale,Hudson,whudson
Zachary,Dylan,Smith,zds
Zachary,Paul,Stump,zstump
这就是代码,现在当我运行脚本并传入一个参数时,它总是会给我列表中的第一行:
$ ./findName.sh zds
$ Adarius Macarthy Adams
$
当我没有通过论证时,它打印出整个列表。我参与了指数,但我无法弄清楚我哪里出错了。