Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in the following Bourne shell script CODE !!!!

Status
Not open for further replies.

hall9

Programmer
Feb 20, 2003
1
US
Hi,
I need some help in the following question. I am trying to read from a file to create users but am stuck in reading the value in double quotes. Following is the file named myfile.txt I am trying to read and looks like.The values are separated by a single space and the one in double-quotes is a single value as it also contains spaces. One line represents one users information.

alc0010 500 "Aaron Lee Conyers" /home/alc0010 /bin/bash
ama0 2 "Test Al-ome" /ama0028 /usr/bin/bash

The following is the code I am trying to use to read the values from the file line by line

####################################
cat myfile.txt |
while IFS=" " read value1 value2 value3 value4 value5
do
......
###################################

But the value3 is the whole value in double quotes, but the space between just take the first word as the value3 instead of the whole in double quotes. Can any one help me out in how to solve this problem and what if there is more than one space between the values. I'll really appreciate for the help.
Thanks

 
This seems to work:

[tt]#!/bin/sh

nawk -F'"' ' { gsub(" ","\\ ",$2) ; print } ' readusers.in | while read user uid gcos home shell
do
echo user=$user
echo uid=$uid
echo gcos=$gcos
echo home=$home
echo shell=$shell
echo
done[/tt]

It presumes that only the GCOS (a.k.a. comment) field in the input file is going to have quotes around it. Try using awk on your system if nawk doesn't exist; awk on Solaris is old and doesn't have "gsub()".

What it does is treats " as the field separator and replaces all spaces in the second field with "\ " (minus quotes) which means the read loop treats them as part of that field. There's no need to specify IFS. Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top