0

Something as simple as this is working surely

#!/bin/bash
touch /sdcard/successful.txt

But its not working in my actual bash command

#!/bin/bash
PACKAGE='com.mixplorer'
if [ $(pidof $PACKAGE) ]; then
  am force-stop com.mixplorer && am start -n com.mixplorer/.activities.BrowseActivity
fi

if [ ! $(pidof $PACKAGE) ]; then
  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
    touch /sdcard/successful.txt
    exit 0;
  fi
done

Explanation:

  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. The above cleaning activity should work only till file manager process is running , Once it stops create a file named successful.txt .Script should end immediately now.

So after i have exited my file manager , the successful.text doesn't gets created in my internal storage for some reason ( which works well if simply touch command is executed )

  • Why don't you move the touch out of the while loop? – Philippos Oct 5 at 19:03
  • 1
    Btw. Indent your code. – Cyrus Oct 5 at 19:05
  • @Philippos regarding while , yes its a loop but i assumed that it doesn't necessarily need a break because it will only last as long as the file manager is active. – Badal Singh Oct 5 at 19:06
  • Still, you can move the touch after the done. You can drop the if then and avoid the (theoretical) chance that file manager process exits between if check and while check. – Philippos Oct 5 at 19:10
  • @Philippos Removing the touch from while loop is causing successful.txt to be created too early . its getting created the first time file manager is opened , i want it to get created after file manager has been closed – Badal Singh Oct 5 at 19:19
0

I fixed it by adding a break after my touch command.

while [ $(pidof $PACKAGE) ]
do
  loopcleaner
  sleep 2

  if [ ! $(pidof $PACKAGE) ]; then
    touch /sdcard/successful.txt
    break
    exit 0;
  fi
done

( although i am very much new to bash and not really confident of myself if this this is the best approach so any further edit/ suggestions to improve it will be lot appreciated )

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.