I am trying to write a BASH script that will issue commands to ADB to remove packages from a list of packages in a file. However, my script leaves the loops that iterates over the file if the script executes any adb command. While ridiculous looking, I added some "Made it here" echo printouts to figure out where things were terminating in my confusion. The script execution seems to be at the end of the script, it just stops looping before the file's end for reasons I don't understand.
My current script with these "hacky" debug prints looks like this:
#!/bin/bash
if [[ -z $1 ]]
then
echo "Enter file name for list of packages to purge"
read PurgeFile
else
PurgeFile=$1
fi
LINE=1
while read -r CURRENT_LINE
do
if [[ $CURRENT_LINE != "" ]]
then
echo "attempting to remove: $LINE: $CURRENT_LINE"
#note, Next line causes the loop to exit if not commented out
adb shell pm uninstall --user 0 $CURRENT_LINE || true
fi
echo "Made it here"
((LINE++))
echo "Made it to outer loop"
done < $PurgeFile
If the line that starts adb shell pm ... is commented out, this script correctly goes through the entire file and prints the "Made it ..." echo-printouts interspersing them correctly. But if that adb command is left in, the script makes it to the first non-empty line in the file and seems to get to the done < $PurgeFile before ending.
This is true no matter what the 'adb' command's output is as I have given it setups where it both succeeds and fails.
Just for reference:
here is the printout from a run where the first adb call is a success, where the rest of the list in the file should also be successful:
$ bash Android_Purge_Script.sh Purge_Packages.txt
attempting to remove: 1: android.autoinstalls.config.samsung
Success
Made it here
Made it to outer loop
here is the pintout from a run where the first 'adb' call is a failure, but where the rest of the list in the file should be successful:
bash Android_Purge_Script.sh Purge_Packages.txt
attempting to remove: 1: android.autoinstalls.config.samsung
Failure [not installed for 0]
Made it here
Made it to outer loop
And below is the first few dozen lines of the printout when using the exact same Purge_Packages.txt file if I comment out the adb call in the script. The output is abridged so as to not waste everyone's time with a couple of hundred lines demonstrating the same pattern again with minor variation in instances over and over.
attempting to remove: 1: android.autoinstalls.config.samsung
Made it here
Made it to outer loop
attempting to remove: 2: com.amazon.appmanager
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 4: com.android.apps.tag
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 6: com.android.bips
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 8: com.android.bluetoothmidiservice
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 10: com.android.bookmarkprovider
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 12: com.android.calllogbackup
Made it here
Made it to outer loop
Made it here
Made it to outer loop
attempting to remove: 14: com.android.chrome
<etc....>
One can see the mostly alternating empty lines in the file correctly iterated over in the above example wherever the four "Made it..." echo-prints occur before the next line with content.
EDIT: Based on the answers, this is apparently a recurring issue with lots Bash scripts when using command line commands that accepts streams. Many of the prior questions that have been answered are linked below, but while the general issue and solution was the same, this question has independent value because none of those particular implementations of the solution worked here (basically to patch in a stream like /dev/null) and instead a different approach was taken here for the implementation that ultimately worked (adjust the command to one that does not take iostream as input.)