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 )

10
  • Why don't you move the touch out of the while loop?
    – Philippos
    Oct 5, 2019 at 19:03
  • 1
    Btw. Indent your code.
    – Cyrus
    Oct 5, 2019 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. Oct 5, 2019 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, 2019 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 Oct 5, 2019 at 19:19

2 Answers 2

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 )

0

In my case we had a touch bash alias function defined as touch -a "$@" which was only updating the access time instead of (in addition to) the modification time.

By default touch will update both of these times, but our bash function was overriding that.

Simply changing the function to touch -am "$@" fixed the problem.

3
  • Why would you do that? If you want a specialised touch command, create something different (atouch, for example) so that these sorts of faults are avoided
    – roaima
    Jun 5, 2021 at 9:48
  • @roaima It's part of our SlickStack project which is automated bash scripts that run themselves via cron jobs, so no user interaction. Point taken, but I'd rather use normal function names so that more users understand what's happening. Jun 6, 2021 at 0:43
  • That's fine; it's your code and your environment. However, speaking from my experience, if you create a function with a similar name to a standard command, people have to go and find out what it does (or if it's well-named they can guess); if you repurpose an existing command name people won't know they even have to look out for its amended definition
    – roaima
    Jun 6, 2021 at 7:12

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.