0

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
+ '[' ']'
$
  • Why is it that you don't write [ -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 -n and -z to test explicitly for non-zero-length or zero-length string. – AlexP Oct 6 '19 at 4:07
  • @AlexP Wasn't aware of that. I just started bash yesterday. Thanks for pointing out , i will try to understand what its about – Badal Singh Oct 6 '19 at 4:08
  • It is about what happens when "$(pidof "$PACKAGE")" is the empty string. (And there is no need for elif [ -n "$(pidof "$PACKAGE")" ]; then -- a simple else will do. – AlexP Oct 6 '19 at 4:12
  • @BadalSingh you asked the same question Here whats the different between those questions? – Shmuel Oct 6 '19 at 8:36
  • @Shmuel There only touch command was broken. Here due to some reason. The script just ends Midway and doesn't even do the loop thing ( and on another note , it looks more similar as a overview because i have added the 3 statements but since i am working on this bash project from few days.. That is something which be common if any question related to this project is asked ) – Badal Singh Oct 6 '19 at 8:38
0

main problem looks like if [ $(pidof "$package") ]

this is what the command do :

  • resolve "$package" , suppose foo
  • run pidof foo and keep output, either empty or "blah"
  • test this output : either [ "blah" ] or [ ]

this is probably not what you want.

(You are mistaking output from a command and return code)

what you want is either (if pidof has a return code true when package is found)

if pidof "$package"

or (if pidof return positive integer when package is found, and 0 if not)

if [ $(pidof "$package") != 0 ]

or (if pidof return nothing when no result is found, as per your trace)

if [ -n "$(pidof "$package")" ]
then (.. found ..)
else ( .. not found ... )
fi

Same goes for while

on a side note, touch after break will not run.

  • Thank you for answering , although i a dont really have any knowledge on bash. I just kept googling and got as far as i could before getting stuck here .. Would appreciate a lot if you can share the corrected script ( can use del.dog ) – Badal Singh Oct 6 '19 at 9:23
  • I changed the pidof... Line as you had mentioned like this - del.dog/ujilicoval. Although its not excuting the while loop and touch command. Debug mode - del.dog/sigexuyoto – Badal Singh Oct 6 '19 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.