Something strange is going on with my Ubuntu 22.04 running version 5.1.16. I am having difficulty doing basic maths in a while loop.
I have a script that runs through all the MKV files in the /Instructions directory. There are thousands of them. The file was to read the titles within the files and clear them out. The code is as follows;
#!/usr/bin/sh
tot=0
spaces=0
blanks=0
text=0
testvar=3
find /Instructions -type f -name '*.mkv' | while read f; do
ls "$f" -al
title=$(mediainfo --Output="General;%Title% %Movie%" $f)
echo "'$f' : "
if [ "$title" = ' ' ]; then
spaces=$((spaces+1))
echo "Spaces"
echo $spaces
# mkvpropedit "$f" --set title=''
elif [ "$title" = '' ]; then
blanks=$((blanks+1))
echo "Blank"
echo $blanks
else
text=$((text+1))
echo "Text"
echo $text
# mkvpropedit "$f" --set title=''
fi
tot=$((tot+1))
echo $tot
testvar=$((testvar+1))
echo $testvar
done
echo "Test Complete"
echo "Total : $tot"
echo "Text : $text"
echo "Spaces : $spaces"
echo "Blanks : $blanks"
echo $testvar
I commented out the active component to test the counts and it lists every file with the settings. But doesn't keep count at all at completion.
It returns this;
SNIP
'/Instructions/TestFlight310120241330.mkv' : Blank
2736
9429
'/Instructions/TestFlight310120241510.mkv' : Blank
2737
9430
'/Instructions/TestFlight010220241005.mkv' : Blank
2738
9431
'/Instructions/TestFlight010220241100.mkv' : Blank
2739
9432
Test Complete
Total : 0
Text : 0
Spaces : 0
Blanks : 0
3
I've executed it via
sh test.sh
bash test.sh
It updates the variables within the while loop but doesn't retain them on exit. Must be something stupid any ideas?
Changed variables and added in comment text. Keeps count in the while loop but loses it when out. I was expecting the variables named outside the loop to be updated whilst in the loop and retained on exit.
I used declare -i tot=0 When run in sh test.sh it was the same except Total : was blank bash test.sh was same as above.
Even tried it on Mint but it was the same.
It seems that this is a known issue with while loops running as a sub process but the solutions given for them do not cover what I am trying to do and I do not have a clue how to fix it. Is it the pipe? Should I make it a pipe to a file and feed the lines in one at a time?
find … | while … do … donetowhile … do … done < <(find …).find | { while do ... done; echo ... ; }whileloop is being run in a subshell; (inbash) variable assignments in the subshell are 'lost' upon exiting subshell and returning to the parent process (ie, variable assignments are not passed 'up/back' to the parent);pmf'ssuggestion is the typical 'fix' (ie, thewhileloop is not executed in a subshell so you'll see your in-loop assignments being maintained after exiting the loop)