I have a problem with one of my bash scripts. I have a variable and a function defined, something super simple:
RESULT='{"single":[],"theme":[],"imagesheet":[]}'
addResult() {
type=$1
filename=$2
OP=".${type} += [\"${filename}\"]"
RESULT=$(echo $RESULT | jq "$OP")
}
I call this addResult
function and check the $RESULT
variable and I can see that it is changed, and the json grows.
However, I also call this function from a loop:
grep -Rh "\.Image(\"" $MY_DIR | while read LINE; do
addResult single "${LINE}"
done
When I check the variable from inside the loop, I can see that the entries are added to the json. However, immediately after the loop ends, the $RESULT
variable loses all of the entries that were added inside the loop. What can I do, to make the changes made inside the loop remain?
EDIT: Thanks to @marcolz I have modified my call to:
RESULT=$(grep -Rh "\.Image(\"" $MY_DIR | while read LINE; do check_imagesheets $LINE; done; echo $RESULT)
Unfortunately, the check_imagesheets
function is quite large and it also contains quite some echo
calls which pollute the final echo $RESULT
. What other approach could I take to mitigate this issue?