BASH + Best method of converting fields to variables
Ow Mun Heng
Ow.Mun.Heng at wdc.com
Sat Jul 17 00:23:15 UTC 2004
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
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
More information about the fedora-list
mailing list