0

I have a script which greps for particular patterns and update them. I also need the count of number of files that are updated. I need to write that count to $GITHUB_ENV

During increment, the variable is assigning correct value. But when writing it to $GITHUB_ENV, it is writing initial value of the variable.

counter=0
grep -ril "search_pattern" ${directory_path%/} | while IFS= read -r file; do
  counter=$((counter + 1))
  echo "Script file: $file"
done
echo "COUNTER=$counter" >> $GITHUB_ENV

While executing the last line - echo, it's always giving 0 for $counter even though I see the $counter getting updated inside the loop.

I used set -x to see it the variable is getting updated inside the loop. and it is updating.

Underoos
  • 4,708
  • 8
  • 42
  • 85
  • Your while loop is executed by a child process and hence uses its own value for `counter`. So don't pipe the output of `grep` into your loop. – user1934428 Jul 13 '23 at 11:40
  • 1
    Don't tag your question by _bash_ and by _sh_, but make up your mind, which shell language you are going to use - in particular since a good solution of your question depends on what shell you are writing the code in. – user1934428 Jul 13 '23 at 11:42

0 Answers0