Re: BASH + Best method of converting fields to variables

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Jul 16, 2004 at 10:29:38PM -0400, Aaron Gaudio wrote:
> Behold, Ow Mun Heng <Ow.Mun.Heng@xxxxxxx> hath decreed:
> > 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
> > 
> > I'm having some trouble getting bash to get the positional fields into variables which I
> > can then plug into useradd
> > 
> > eg:
> > pseudo code
> > for each line in input file
> >   do
> > 	username=$1
> > 	name=$2
> > 	useradd -c "$2" $1
> >   done
> > 
> > I ended up using awk, which works but seems complicated
> > 
> > 	eval `awk -F ':' \
> >         '{printf ("useradd -c \"%s\" ",$2)}; \
> >         length($3)>0 {printf ("-g %s ",$3)}; \
> >         length($4)>0 {printf ("-s %s ",$4)}; \
> >         length($5)>0 {printf ("-d %s ",$5)}; \
> >         {printf ("%s\n;",$1)};' $1`
> > 
> > Can it be done using bash itself? (without calling awk)
> > Actually I was hoping I can get awk to get the fields for the variables
> 

How about using the IFS variable for this ?
set IFS to ':' and run a for...done loop over each line of the input file.

   Karsten

--
 Karsten Hopp <karsten@xxxxxxxxx>   GPG 1024D/70ABD02C
 Fingerprint D2D4 3B6B 2DE4 464C A432 210A DFF8 A140 70AB D02C
 Red Hat Deutschland, Hauptstaetter Str.58
 70178 Stuttgart, Tel.+49-711-96437-0, Fax +49-711-96437-111



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux