Operating System - Linux
1748347 Members
5321 Online
108762 Solutions
New Discussion юеВ

Re: for-do-done for 2 Fields

 
SOLVED
Go to solution
Robin King_1
Regular Advisor

for-do-done for 2 Fields

Oh scripting guru's out there.

I need to create about 130 user accounts on a new server. The App Team have provided me with a file containing, what will be their 5 digit uid, and a comment field containing thier full name, and location (comma delimited).

Looks a bit like this:
12345 First Surname,UK/Town Name

I'm a bit stumped as to how I'm going to achieve the following:

useradd -u -g -d /apps/gns -s /apps/gns/bin/pfash -c <uid><BR /><BR />I'm not too sure how to feed the fields into a for-do-done loop. Do I also need to delimit the fields with an explicit character, as I currently have 4 fields in there delimited by a space. <BR /><BR />Any help would be appreciated...my head hurts. <BR /><BR />
6 REPLIES 6
curt larson_1
Honored Contributor

Re: for-do-done for 2 Fields

something like this should work ok for you

cat file |
while read uid rest
do
print "uid = $uid and rest = $rest"
done
Mel Burslan
Honored Contributor
Solution

Re: for-do-done for 2 Fields

easiest way is to use a while-do-done construct in my opinion, as follows:

cat myfile | while read line
do
field1=`echo $line|cut -d, -f1`
field2=`echo $line|cut -d, -f2`
UID=`echo field1|awk {'print $1'}`
NameSurname=`echo field1|awk {'print $2" "$3'}`
LOCATION=$field2

useradd -u $UID -g $GID -d /apps/gns -s /apps/gns/bin/pfash -c "${NameSurname},${LOCATION}" $username

# on this statetement, you need to set the username and GID variables
# before executing the last command.

done


Hope this helps
________________________________
UNIX because I majored in cryptology...
curt larson_1
Honored Contributor

Re: for-do-done for 2 Fields

if you put the field with spaces as the last field you won't have to delimit the fields.

with the read command the first field is assigned to the first name, the second field to the second name, etc., with the leftover fields assigned to the last name.
Robin King_1
Regular Advisor

Re: for-do-done for 2 Fields

Thanks all, that really helps. I find it a lot easier reading them than contructing them.
James R. Ferguson
Acclaimed Contributor

Re: for-do-done for 2 Fields

Hi Robin:

Curt's suggestion is a good one for its simplicity. I'll add another useful tip:

The shell's IFS (Inter-Field-Separater) is designed to be leveraged for problems like this. You can set the value of 'IFS' to any character you choose. For example, if the colon (":") character delmited your fields:

#!/usr/bin/sh
OLDIFS=${IFS}
IFS=":"
while read A B C
do
echo "${A} ${B} ${C}"
done < myfile
IFS=${IFS}

Regards!

...JRF...

Andy Torres
Trusted Contributor

Re: for-do-done for 2 Fields

Assuming you edit your file to read like this:
<"COMMENT WITH SPACES">
For example,

/tmp/userlist:
user1 111 333 /home/home /shell "comment with spaces"
user2 222 433 /home/home /shell "comment with spaces"

Try this:

cat /tmp/userlist | while read USERNAME UID GID HOME SHELL COMMENT
do
useradd -u $UID -g $GID -d $HOME -s $SHELL -c $COMMENT $USERNAME
done