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 You could try this: read -d: USERNAME read -d: NAME read -d: GROUP etc.. Keep in mind that when using -d, read will not treat a newline as a delimiter, so you have to use more trickery if you want optional fields. > > psuedo code > username = awk -F: {'print $1}' > name = awk -F: {'print $2}' > > Any takers?? > > 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. > > > > -- > Ow Mun Heng > Fedora GNU/Linux Core 2 on D600 1.4Ghz CPU kernel > 2.6.7-2.jul1-interactive > Neuromancer 17:13:44 up 5:56, 10 users, load average: 1.65, 1.32, 0.97 > > > -- > fedora-list mailing list > fedora-list@xxxxxxxxxx > To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list -- prothonotar at tarnation.dyndns.org "Every man is a mob, a chain gang of idiots." - Jonathan Nolan, /Memento Mori/
Attachment:
pgpkICAe4gs8e.pgp
Description: PGP signature