Explanation of what i am trying to achieve:
1) As soon as script is executed , first check if file manager with package name 'com.mixplorer' is active If yes , then force stop it and open it again If no , then just simply open it
2) Now that the file manager is already opened , keep deleting a file named 'log.txt' as defined by loopcleaner every 10 seconds in a loop forever as long as file manager process is running
3) Only after file manager is no longer active , end the loop cleaning process from step 2 and create a file named successful.txt Now everything is done , script may end
Here's my script
#!/bin/bash
PACKAGE='com.mixplorer'
if [ $(pidof $PACKAGE) ];
then
am force-stop com.mixplorer && am start -n com.mixplorer/.activities.BrowseActivity;
else
am start -n com.mixplorer/.activities.BrowseActivity;
fi
loopcleaner()
{
rm -rf /sdcard/log.txt
}
while [ $(pidof $PACKAGE) ];
do
loopcleaner;
sleep 2;
if [ ! $(pidof $PACKAGE) ];
then
break
touch /sdcard/successful.txt
fi
exit 0;
done
Here's the debug output , which clearly shows the script just abruptly stops midway doesn't do the while loop when package is active and touch command after package is no longer active ( obviously i manually closed the file manager to give this a chance be to triggered )
$ su -c sh -x /sdcard/tester.sh
+ PACKAGE=com.mixplorer
+ pidof com.mixplorer
+ '[' ']'
+ pidof com.mixplorer
+ '[' ! ']'
+ am start -n com.mixplorer/.activities.BrowseActivity
Starting: Intent { cmp=com.mixplorer/.activities.BrowseActivity }
+ pidof com.mixplorer
+ '[' ']'
$
[ -n "$(pidof "$package")" ]and[ -z "$(pidof "$package")" ]like so many beginner's books have tried to teach you? Always quote your variables. Always quote the result of command substitution. Always use-nand-zto test explicitly for non-zero-length or zero-length string."$(pidof "$PACKAGE")"is the empty string. (And there is no need forelif [ -n "$(pidof "$PACKAGE")" ]; then-- a simpleelsewill do.