Bash:从变量中读取行。嵌套循环

时间:2011-10-20 10:58:23

标签: bash

我能够在第一个循环中逐行读取,但第二个循环一次返回所有行 我希望第二个循环逐行读取similsr到外部循环。我该如何解决这个问题?

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

2 个答案:

答案 0 :(得分:1)

您应该使用-a而不是-r。

示例:

{0,244}$> echo "a b c" | { read -a j; echo ${j[0]}; echo ${j[1]}; echo ${j[2]}; }
a
b
c

答案 1 :(得分:0)

这对我有用,如下

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

参考:http://mywiki.wooledge.org/BashFAQ/001 &lt;&lt;&lt;&lt;&lt;构造在链接上被称为“here string”