0

I am able to read line by line in the first loop, but 2nd loop returns all lines at once.
I want the second loop to read line-by-line similsr to the outer loop. How can I resolve this?

firstlist=`<some command that returns multi-line o/p>`
if [ "x$firstlist" != "x" ] ; then
    printf %s "$firstlist" |while IFS= read -r i
    do
        secondlist=`<some command that returns multi-line o/p>`
        if [ "x$secondlist" != "x" ] ; then
            printf %s "$secondlist" |while IFS= read -r j
            do
                doverify $i $j
            done
        else
            echo "Some message"
        fi
     done
else
    echo "some other message"
fi
3
  • Hi @Rajshekar Iyer, with your code for every i you get all the j. What exactly are you trying to achieve? Commented Oct 20, 2011 at 11:09
  • Hi @DimitreRadoulov, I want the second loop to read one line at a time too instead of the complete variable. Commented Oct 20, 2011 at 11:20
  • For me your code does exactly this, perhaps you should check the secondlist content? Commented Oct 20, 2011 at 11:28

2 Answers 2

1

You should use -a instead of -r.

Example:

{0,244}$> echo "a b c" | { read -a j; echo ${j[0]}; echo ${j[1]}; echo ${j[2]}; }
a
b
c
Sign up to request clarification or add additional context in comments.

Comments

0

This worked for me as follows

firstlist=`<some command that returns multi-line o/p>`
  if [ "x$firstlist" != "x" ] ; then
  while IFS= read -r i
  do
    secondlist=`<some command that returns multi-line o/p>`
    if [ "x$secondlist" != "x" ] ; then
        while IFS= read -r j
        do
            doverify $i $j
        done <<< "$secondlist"
    else
        echo "Some message"
    fi
   done <<< "$firstlist"
else
  echo "some other message"
fi

Reference: http://mywiki.wooledge.org/BashFAQ/001 The <<< construct is called "here string" per the link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.