1

I wanted to create a bash script to achieve a particular task although i have no knowledge of bash. Lazily asking it here directly wouldn't have have explained my question well , so i just kept struggling for 1 hour and got as far as i could so that there's atleast a basic structure of my code ( even tho possibly broken but still it would easier to get my question so more people will be willing to help ).

First lemme show exactly the error which i am getting

$ su
$ sh "/sdcard/tester.sh"
/sdcard/tester.sh[2]: syntax error: 'if' unmatched

Code below

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

loopcleaner()
{
rm -rf /sdcard/log.txt
}

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

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

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' process is already 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 process is no longer active , end the loop cleaning process from step 2 and create a file named successful.txt . After creating this file , script should end / kill its own process

  • 2
    missing fi for the first if – glenn jackman Oct 5 at 17:17
  • @glennjackman Yes thanks that did it – Badal Singh Oct 5 at 17:19
0

Add fi at the end of this section, like this:

if [ $(pidof $PACKAGE) ]; then
am force-stop com.mixplorer
am start -n com.mixplorer/.activities.BrowseActivity
fi
  • Yes that fixed it. Although i can't trust myself that i have done everything else mentioned in my three statements correctly , can you confirm it once please – Badal Singh Oct 5 at 17:17
  • Looks fine to me. – ajgringo619 Oct 5 at 17:20
  • everything is working well now except that successful.txt is not getting created .i will try fine tuning it. Maybe i need to keep it like - if file manager is removed from recents , force stop it. Then create that successful. Txt file ... In any case marking your answer as accepted. I will ask a new one later for another purpose if i have to seek some help – Badal Singh Oct 5 at 17:31
  • I'm an idiot! Use touch /sdcard/successful.txt instead of mkdir. – ajgringo619 Oct 5 at 17:33
  • @ajgringo619 Also, you can't expect to run touch or mkdir just after break. – Kusalananda Oct 5 at 17:35

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.