0

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

3
  • 2
    missing fi for the first if Commented Oct 5, 2019 at 17:17
  • @glennjackman Yes thanks that did it Commented Oct 5, 2019 at 17:19
  • Always paste your script into https://shellcheck.net, a syntax checker, or install shellcheck locally. Make using shellcheck part of your development process.
    – waltinator
    Commented Oct 31, 2021 at 2:52

1 Answer 1

1

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
6
  • 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 Commented Oct 5, 2019 at 17:17
  • Looks fine to me. Commented Oct 5, 2019 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 Commented Oct 5, 2019 at 17:31
  • I'm an idiot! Use touch /sdcard/successful.txt instead of mkdir. Commented Oct 5, 2019 at 17:33
  • @ajgringo619 Also, you can't expect to run touch or mkdir just after break.
    – Kusalananda
    Commented Oct 5, 2019 at 17:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .