INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

For loop curiosity

For loop curiosity

(OP)
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

RE: For loop curiosity

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 


RE: For loop curiosity

Sam, you don't need to unset IFS as you read a single variable ...

RE: For loop curiosity

Furthermore, the default IFS is Space or Tab or LineFeed.

RE: For loop curiosity

(OP)
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

RE: For loop curiosity

(OP)
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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close