• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Root [International] Script trouble

Vampirex

Newbie
Hi All,
I encounter a strange thing: I want to manage my backup automatically,and I try to write a script to do that, I am going by after step.
My first attempt was to delete older files and directory, I keyed in every individual statement in terminal mode and it works well, but when I want to record as a script, it runs without any trouble, but nothing is done. Here is my commands:
#!/bin/sh
su
chmod 777 bb.sh
rm /mnt/extSdCard/clockworkmod/backupold/*
rmdir /mnt/extSdCard/clockworkmod/backupold/
I have set the directory where the script run as working directory with the CD command.
Any idea ?
Many thanks.
 
Hi All,
I encounter a strange thing: I want to manage my backup automatically,and I try to write a script to do that, I am going by after step.
My first attempt was to delete older files and directory, I keyed in every individual statement in terminal mode and it works well, but when I want to record as a script, it runs without any trouble, but nothing is done. Here is my commands:
#!/bin/sh
su
chmod 777 bb.sh
rm /mnt/extSdCard/clockworkmod/backupold/*
rmdir /mnt/extSdCard/clockworkmod/backupold/
I have set the directory where the script run as working directory with the CD command.
Any idea ?
Many thanks.

That "su" spawns off a new shell. As a result, the commands that follows will only execute after the "su" exits.

You need to put the 3 commands after the "su" into your script and execute the script using "su".

Try the following to create a script that removes your old backups:

Code:
shell@android:/ $ su
shell@android:/ PS1='# '
# cd /mnt/extSdCard/clockworkmod
# echo '#!/system/bin/sh' > clrbackupold.sh
# echo 'rm -rf /mnt/extSdCard/clockworkmod/backupold/*' >> clrbackupold.sh
# exit

Now you can execute the script via:
Code:
shell@android:/ su -c 'sh /mnt/extSdCard/clockworkmod/clrbackupold.sh'

The above command means:
"Switch to root and execute sh (shell interpreter) to execute the script clrbackupold.sh"

See ya...

d.c.
 
How are you running the script? Just via term?
I am running this on the console via term for testing and debugging. The goal is to run by a scheduler like Tasker, Locale or something similar for my weekly backup and have always the last version on my sd card.
BR.
Daniel.
 
How are you running the script? Just via term?

That "su" spawns off a new shell. As a result, the commands that follows will only execute after the "su" exits.

You need to put the 3 commands after the "su" into your script and execute the script using "su".

Try the following to create a script that removes your old backups:

Code:
shell@android:/ $ su
shell@android:/ PS1='# '
# cd /mnt/extSdCard/clockworkmod
# echo '#!/system/bin/sh' > clrbackupold.sh
# echo 'rm -rf /mnt/extSdCard/clockworkmod/backupold/*' >> clrbackupold.sh
# exit
Now you can execute the script via:
Code:
shell@android:/ su -c 'sh /mnt/extSdCard/clockworkmod/clrbackupold.sh'
The above command means:
"Switch to root and execute sh (shell interpreter) to execute the script clrbackupold.sh"

See ya...

d.c.
Thank for helping.
As I explained this script is intended to be launched by a scheduler, so all the statements have to be inside the script.
So, despite I didn't all understand, l tried exactly what you propose, but I get the message:
"...cannot execute - Permission denied"
I think that the trouble is probably around the level of authorization and the reset of some shell parameter, but I am not yet enough sharp on the subject to be able to find something.
BR.
Daniel.
 
Nothing works. I really don't understand, it is very difficult because no error message, no direction to go... A lot of statements are different from Linux and, of course Unix, I don't find any reference book or doc.
Discouraging.
 
You don't need su to use rm or rmdir on extSdCard.

I just tested this.

As you can see, I created Test/File and ran the 2 commands. I recreated Test/File.

Then I created Script.sh with nothing else in it and ran from term. .

Worked both times.

Screenshot_2012-09-18-17-17-45.png
 
Also, if I create a task in tasker > script > command

sh /mnt/extSdCard/script.sh

And run it, it also works.

FYI, tasker has a "use root" tick box in the script task, however it is not needed here
 
Hello SUroot,
Here is my script:

#!/bin/bash
# My first script
echo "Hello World!"
echo "HELLO Saint Aubin..."
echo "Hello $USER."
echo "Today is $(date)"
echo "Current working directory : $(pwd)"
rm -r /mnt/extSdCard/clockworkmod/backupold/*
rmdir /mnt/extSdCard/clockworkmod/backupold/
exit

Screenshot is enclosed.
I tried a lot of of things, but mainly I wrote the script under UtraEdit in W7, wrote to the phone, enabled this script called "hello.sh" with chmod 755...
Nothing worked, I even don't understand what happened, because the warning (when issued) are inconsistent. Finally, I found that I can list variables, it works and send message on the terminal. But as soon as I put an action statement like RM or RMDIR it doesn't work and mainly I don't have any track to make the problem determination.
Perhaps I don't know enough (I am a beginner with Android) and made a big mistake or missing someting. But look at the screen, on the rm for emptying the 'backupold' folder, it answers 'no such file or directory', but after that on the rmdir it says 'directory not empty'. If I use the terminal to enter these commands (excep -r in the rm that I didn't test), all work OK.
I really don't understand.
Now it is half passed midnight here in France, time to go to bed. Perhaps someone will give me a track to follow for tomorrow (I hope...).
Thank for you help, I need it...
BR.
Daniel.

BTW I apologize for my English, I hope it is understandable.
 

Attachments

  • Screenshot_2012-09-19-00-05-16.png
    Screenshot_2012-09-19-00-05-16.png
    128.7 KB · Views: 73
Don't create scripts in windows. Just create a text file on your phone.

Start simple. Create exactly same as me then get that working. Then next step
 
There are lots of things going on that are tripping you up.

  • /mnt/extSdCard is mounted with a setting that tells the OS to ignore execution of programs/scripts. Therefore, adding execution permission (chmod 755 ...) will not work. You will need to run the script by executing the shell interpreter (/system/bin/sh) with the name of the script as the argument. Hence, my example of executing your script via: "sh /full/path/to/your/script"
  • Android runs everything using separate user accounts. Android also has separate ownerships and permissions set for each directory. In your screenshot, the user "app_187" is used to execute all your commands. It might be that "app_187" does not have the permission to remove files/directories from the /mnt/extSdCard/clockworkmod directory. This was why I suggested you use the "su" program to execute your script with superuser credentials (i.e. su -c 'sh /full/path/to/your/script').
  • Android's Korn Shell equivalent (mksh) wants end-of-line (EOL) markers to be <NL> (like most UNIX-based systems) not <CR><NL> (all Windows systems). You will need to force UltraEdit to strip off the <CR> and only use <NL> as the EOL. If you forget and leave the EOL windows-style, then many of your scripts that rely on IF...THEN...ELSE...FI will fail.

Ok. Here's my VERY LONG suggestion:
  1. Start by creating a stub for your backup script and put it into /mnt/extSdCard/clockworkmod (I'll assume the filename is bb.sh). Inside bb.sh will be:
    Code:
    if test ! -d /mnt/extSdCard/clockworkmod; then
      echo "CWM directory not found on External SD Card...exiting"
      exit 1
    fi
    cd /mnt/extSdCard/clockworkmod
    if test -d backupold; then
      echo "Removing all old backups from backupold"
      rm -rf backupold
    fi
    exit 0
  2. Create a wrapper script that will execute your bb.sh with superuser permissions and put it into /mnt/extSdCard/clockworkmod (I'll assume the filename is sudo.sh). Inside sudo.sh will be:
    Code:
    if test "${1%.sh}" != "${1}"; then
      exec su -c "/system/bin/sh $@"
    else
      exec su -c "$@"
    fi
  3. Verify that bb.sh and sudo.sh will work by creating a /mnt/extSdCard/clockworkmod directory and put a file in that directory. Then, from the command line, execute this command:
    Code:
    sh /mnt/extSdCard/clockworkmod/sudo.sh /mnt/extSdCard/clockworkmod/bb.sh
    You should get the message "Removing all old backups from backupold".
  4. If the above works, you can add more commands before the "exit 0" line of bb.sh.
  5. If you have some type of cron/scheduler, you can use the "sh /mnt/..../sudo.sh /mnt/..../bb.sh" line from above as the recurring command.

See ya...

d.c.
 
Hello imchandave,
As a starting point I got all your code, put in txt file in W7 (the kb is really too difficult on the SGS3), then wrote it on the S3 and rename .txt to .sh.
I ran as you suggest and I got a "syntax error: "fi" unexpected".
Of course nothing is done.
What is the purpose of "Fi" ?
BR.
Daniel.
 
Hello imchandave,
As a starting point I got all your code, put in txt file in W7 (the kb is really too difficult on the SGS3), then wrote it on the S3 and rename .txt to .sh.
I ran as you suggest and I got a "syntax error: "fi" unexpected".
Of course nothing is done.
What is the purpose of "Fi" ?
BR.
Daniel.

As I stated in my previous post, you need to make sure UltraEdit writes out the script file using UNIX end-of-line markers. I can't tell you how to do that since I don't use UltraEdit.

See ya...

d.c.
 
Hello SURoot,
I tried exactly as you did and ... it worked very well :).
Now I have to enhance my script to only delete if the old directory exists.
Still stay a mystery: why it didn't work before...
BR.
Daniel.
 
As I stated in my previous post, you need to make sure UltraEdit writes out the script file using UNIX end-of-line markers. I can't tell you how to do that since I don't use UltraEdit.

See ya...

d.c.
You are right, I re-edited the file on the S3, deleted all the end-of-line markers and it works like a charm.
Now, I have to complicate the things, but the programming logic doesn't scare me, what I don't like are all these small things on the way. I am fundamentally a mainframe guy and was growing up at the IBM baby bottle, so in Windows, Unix, Linux and now Android all is not very different (except probably the serious of implementation), but for a same thing, there is every time a new name. It is exhausting.
Anyway thank you for your help, it is a very good forum.
BR.
Daniel.
 
Hello SURoot,
I tried exactly as you did and ... it worked very well :).
Now I have to enhance my script to only delete if the old directory exists.
Still stay a mystery: why it didn't work before...
BR.
Daniel.

Yes of course. The script is 2 lines and is very simple. It needs nothing else other than the rm and rmdir commands. It needs no elevated permissions or anything of that nature. No need to over complicate it
 
Hi,
I have a little complicated because, I test if the folder is present, empty etc... without stopping the backup. But as soon as I found how to run correctly a script, all went right.
BR.
Daniel.
 
Back
Top Bottom