0

I want to search a repository file to see if I have a file in my directory. To do this, I loop the directory hopefully containing the file I am looking for, and I check to see if that file is in my repository.txt. If it is in there, I change a boolean, and then check the value of that boolean after I compared all the files in the repository.

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 

Is there a way I can persist the changing boolean value, or is there another, smarter way of doing this? Please let me know if you have any questions.

0

1 Answer 1

4

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?"

while read ...
do
 ...
done < <(cut ...)
Sign up to request clarification or add additional context in comments.

1 Comment

Since $repo doesn't seem to change, you can save the result of cut to a variable and avoid the process substitution. field=$(cut -d, -f "$repo"); for file in *; do while read line; do ...; done < "$field"; echo "..."; done

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.