Shell 编程:在内循环之外保留字符串

问题描述 投票:0回答:1

全部:

我正在尝试使用两个循环来检查 $new 字符串是否已存在于存在列表中。

但是,$p 字符串似乎并没有在内循环之外持续存在以强制外循环中断。

#!/bin/sh

p=0

while read new; do
   while read exist; do
      echo "$new - $exist"

         if [ "$new" = "$exist" ]; then
            #echo "$new - $exist"
            p=1
            echo "$p"
            break
         fi
   done<existlist
   if [ $p -ne 1 ]; then
      echo "$p"
      break
   fi
done<newlist

echo "#$new#"

这可能是一个范围问题,但我不确定。

非常感谢您的帮助。

谨此,

加里

loops shell scope
1个回答
0
投票

为什么不使用 grep 呢?

#!/bin/sh 阅读 MYSTR 时;做 grep "$MYSTR" 存在列表 如果 [ $? -eq 0];然后 echo "$MYSTR 存在于新列表中" 休息 菲 完成

© www.soinside.com 2019 - 2024. All rights reserved.