首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash变量作用域

Bash变量作用域
EN

Stack Overflow用户
提问于 2012-09-10 08:00:11
回答 1查看 142关注 0票数 0

我想搜索存储库文件,看看我的目录中是否有文件。为此,我循环包含我正在查找的文件的目录,并检查该文件是否在我的repository.txt中。如果它在其中,我将更改一个布尔值,然后在比较存储库中的所有文件后检查该布尔值的值。

代码语言:javascript
复制
for file in *
 do
    inThere=false        
    cut -d, -f1 $repo | while read line
      do
        echo "comparing "$line" == "$file" "
        if [ "$line" == "$file" ]; then
          inThere=true
          echo "I got tripped!!" #inThere is true
          echo "in there is $inThere"
        fi
    done
          echo "in there is $inThere" #inThere is false        
  done 

有没有一种方法可以持久化不断变化的布尔值,或者是否有另一种更智能的方法来实现这一点?如果你有任何问题请告诉我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-10 08:11:52

BASH FAQ entry #24: "I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?"

代码语言:javascript
复制
while read ...
do
 ...
done < <(cut ...)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12343981

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档