Re: BASH + Best method of converting fields to variables

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

 



Write it in perl.  ;)

On Sat, 17 Jul 2004 13:03:44 -0700, Ow Mun Heng <ow.mun.heng@xxxxxxx> wrote:
> On Sat, 2004-07-17 at 05:43, Karsten Hopp wrote:
> > 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.
> 
> I'm not sure how that can be used.
> 
> One other thing which I found is that by using awk, I'm actually
> processing the file all at once. (as the awk code above shows). I'm not
> sure how I can get BASH to actually process it line-per-line like tcsh's
> foreach.
> 
> Maybe I can learn up on using awk's $NR variable, to get it to
> auto-increment so that I read it line-by-line and process it that way.
> 
> Right now, my random password generator function is making the _same_
> default password for _all_ users. That's Not too great.
> 
> Dang.. the more I write, the more complicated I'm making it.
> 
> Thanks for the help
> 
> >
> > --
> >  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
> 
> --
> Ow Mun Heng
> Fedora GNU/Linux Core 2 on D600 1.4Ghz CPU kernel
> 2.6.7-2.jul1-interactive
> Neuromancer 12:57:04 up 14:59, 7 users, load average: 0.61, 0.97, 0.83
> 
> 
> --
> fedora-list mailing list
> fedora-list@xxxxxxxxxx
> To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list
> 


-- 
Charlie Heselton
Network Security Engineer



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

  Powered by Linux