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

For loop curiosity 1

Status
Not open for further replies.

squash

MIS
May 21, 2001
99
0
0
US
I have been writing a large script with many functions inside it.
At one point I had need to capture some text data and print to the screen.
I was surprised by the results I got and after some research time decided to change tactics.
Meaning I used another method to get the results I need.
However now that the script is finished and working I went back to my "for loop" issue to see if
I could figure out a way to make this work for future uses.
Maybe what I want/expect is not possible in this case?
It seems that "for" is parsing each item in the text file with space/tab separations.

I expected "new line" as the parse for each item.


I have two files:
1. Simple text file containing multiple lines.
Each line contains a command with arguments
2. A script that uses this text file

Text file = list.txt
Here is a two line sample
cat list.txt
./syncNode.sh ndm_${CELL} $PORT
./syncNode.sh ndm_${CELL}q $PORT

Script file = script.ksh
for i in `cat list.txt`
do
echo ""
echo "$i"
done

output of script.ksh

./syncNode.sh

ndm_${CELL}

$PORT

./syncNode.sh

ndm_${CELL}q

$PORT


Results I expected:

./syncNode.sh ndm_${CELL} $PORT

./syncNode.sh ndm_${CELL}q $PORT


Now I know in my example above the "for" loop is redundant as I could just cat the file.
However in my original script I was trying some more complex things inside the loop.
The "for" loop was NOT my best option, but at the time seemed prudent.
I was surprised at the output I got and now it is just a curiosity, not a needed exercise.
I am not looking for another way to accomplish, just like to learn how/why I got unexpected results.
The best I can figure is "for" ignores the carriage return and treats the file no different from:
for i in "1 2 3 4 5"
Be glad to hear any thoughts.
Thankx


As always we thank you for your support
Doug
 
The 'for ... in .. do ... done' parse words, not lines.
You may loop by rows like this:
Code:
while read i; do
  echo ""
  echo "$i"
done < list.txt

Furthermore, as you have variables (ndm_${CELL}) you may consider the eval builtin.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
To make it read the full line, put this before the loop...

Code:
unset IFS

The default IFS (Inter Field Separator I think), is whitespace (space or tab). You can also set it to something specific to parse fields as you need it. Example...

Code:
#!/bin/ksh

# Sample: oracle:x:100:100:Oracle Account:/home/oracle:/bin/ksh

# This makes the read get the full line
unset IFS

while read LINE
do
        # This makes the read parse the colon separated fields
        IFS=:

        print "${LINE}" | read USER PASS UID GID NAME HOMEDIR HOMESHELL

        print "${USER} = ${NAME}"
done < /etc/passwd


 
Sam, you don't need to unset IFS as you read a single variable ...
 
Furthermore, the default IFS is Space or Tab or LineFeed.
 
Phv,
Your solution is one I had not considered as I used grep in my original solution.
Although looking at my code, i can see you use for future updates of this script.

Sambones,
I played with the IFS variable for my own leaning and found it difficult to wield the results I had expected.
I will certainly do more reading on this one again for possible future use.

Thankx
Doug


As always we thank you for your support
Doug
 
just playing around a little I find that IFS will work like so:

IFS=$'\n'
for i in `cat list.txt`
do
echo ""
echo "$i"
done

Always fun to learn somthing as it is too easy to stick with what you know.




As always we thank you for your support
Doug
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top