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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For Loop returns only first colum

Status
Not open for further replies.

dUbbsNIX

MIS
Jul 10, 2003
70
GB
Here is my problem.

I create a .lst file containing the following:

-rw-rw-r-- 1 dvpears prism 19040 Sep 25 10:56 dax_e_ic_20030828.csv
-rw-rw-r-- 1 dvpears prism 19042 Sep 25 10:56 dax_e_ic_20030829.csv
-rw-rw-r-- 1 dvpears prism 71 Sep 25 10:56 dax_e_in_20030828.csv
-rw-rw-r-- 1 dvpears prism 71 Sep 25 10:56 dax_e_in_20030829.csv

Then I want to cycle through the contents of the .lst file, record by record, assigning each column into a variable.

My loop would be:
for rec in $(cat $LIVE_WORK/serverfiles.lst)
do
.
.
.

The problem I have is that for the 1st row in the .lst file, $rec is returning the following:
-rw-rw-r--
instead of
-rw-rw-r-- 1 dvpears prism 19040 Sep 25 10:56 dax_e_ic_20030828.csv

I think it is because of the spaces between each data item. How do I get the whole row assigned in $rec?

Any help greatly appreciated.
 
Try...

while read rec
do
:
done < $LIVE_WORK/serverfiles.lst

 
Try something like this:
savIFS=&quot;$IFS&quot;;IFS=$(echo &quot;\n\r&quot;)
for rec in $(< $LIVE_WORK/serverfiles.lst)
do
...
done
IFS=&quot;$savIFS&quot;

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
With the 'while read' solution, you can assign each column to a variable :
[tt]
while read mode links owner group size time1 time2 time3 name
do
. . .
done < $LIVE_WORK/serverfiles.lst
[/tt]

Jean Pierre.
 
With either solution, you could assign each column into an array, e.g...

while read rec
do
set -A col -- $rec
echo name is ${col[8]}
done < $LIVE_WORK/serverfiles.lst
 

Thanks to everyone. This has solved my problem and in super fast time.

Thanks again,

Dubbs.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top