BASH + Best method of converting fields to variables

Ow Mun Heng Ow.Mun.Heng at wdc.com
Sat Jul 17 19:55:48 UTC 2004


On Fri, 2004-07-16 at 17:35, Rick Stevens wrote:
> Ow Mun Heng wrote:
> > Guys,
> > 
> > 	Writing a batch_create_new_user script.
> > input is a file with this format
> > 
> > Username:Full Name:Default Group:Default Shell:Home Directory:Password
> >  
> > and the using those fields ($1 $2 $3 etc) and passing then to the useradd program

> > BTW, what's the best way to check the number of fields in the input file?
> > I was thinkning of counting how many ":" there are in each line. But can't
> > figure out how to actually _do_ it.
> 
> awk's "$NF" variable contains the number of fields in the current input
> record.

Hmm.. that's a good tip..

I figure that since I am supposed to have like 6 fields per line, I can do this

NoRecords=`cat $1 | wc -l`
TotalNoFields=`awk -F':' '{ total = total + NF }; END {print total}' $1`
let FieldsPerLine=$TotalNoFields/$NoRecords
if [ $FieldsPerLine -ne "6" ]; then
	echo "Incorrect Number of Fields"
	usage
	exit 1
fi

Cool.. That's one thing solved.





-- 
Ow Mun Heng
Fedora GNU/Linux Core 2 on D600 1.4Ghz CPU kernel
2.6.7-2.jul1-interactive 
Neuromancer 12:34:55 up 14:36, 6 users, load average: 1.06, 0.89, 0.82 





More information about the fedora-list mailing list