Scripts for repetitive terminal commands like - reboot recovery

Ridgeland

Member
Dec 14, 2010
285
9
In Linux scripts are great for automating a series of terminal commands. Back in MS-DOS days I used batch files to automate a series of commands.
In Android you can also create scripts to automate a series of task - no typing just click to select - and it runs.
My MID7015 is rooted with a Superuser.apk. I'm using gscript app to run the script.
Here are the three steps I want to automate:
cp /sdcard/clockworkmod/recovery.img /sdcard/recovery.img
flash_image recovery /sdcard/recovery.img
reboot recovery
This simple script reboots my Coby to the Clockworkmod menu. No holding buttons required.

I try to keep root of /sdcard/ nearly empty and store files safely in subdirectories like /sdcard/clockworkmod (which is where the backups will be stored too.) So the first command is just to copy my Clockworkmod recovery.img file to the root of /sdcard/.
The second command flashes the image, the third command reboots the system.

So more of the nitty gritty. gscript has a text editor which I didn't like at all so I edited using TextEdit.
Install the app gscript
Launch TextEdit and type in the three commands above.
Save the file as /sdcard/gscript/Boot_Recovery.sh
(has to end with .sh or it will not run)
Launch gscript
Click on the menu button, select "Add Script"
click on [Load file]
Select the Boot_Recovery.sh file
Leave [x] Needs SU? turned on
click on [Save]

Now see how easy it is to boot to recovery.img
Just click on the script.
On my tablet a SuperUser screen pops up requiring my permission to run the script as superuser.
I click OK - you can set it to remember so this does not pop up again.
In seconds my tablet is booting the recovery image and I can select "backup" "restore" etc.

Sorry to be long winded but new bees need detail. It seems hard the first time but you'll appreciate it the second time you just click and reboot.

What other scripts have you created? Need to see?
 
Last edited:
If clockwork recovery is flashed correctly you would not need a script to flash it every time you want to boot into recovery. There are several reboot apps on the market that allow 2 click reboots, either into recovery or normal.

Sent from my SPH-M900
 
This thread was meant to help others see how to write and use scripts.
The example of recovery.img was meant only as an example.
If I had used an example with just "ls" that might have been too simple but would have still presented the concept.
Next I'll toy with scripts that install a list of apps (to use after a factory reset).
 
Ok. I got ya. I had a hard time getting clockwork to stick until I found the "mv" command. When I saw this I was thinking that some people may think this is a workaround for getting clockwork to stick, which would be a bad idea since you would not be able to access recovery if you were to get stuck in a boot loop or otherwise brick your tablet...
 
Funny... I was just thinking about batch files for Linux today...
Thanks for the tutorial, keep it coming.
What app are developers using to create Apps?
 
willber,
I had you in mind when I wrote this because you said you were using multiple backups and installs to test with and without Market.
Rebooting Recovery.img will be easy this way.
Don't you have Android Developers SDK?
What is Android? | Android Developers
Haven't you written your first helloworld.apk yet?
Are you a Linux user?
 
Yes the multiple rebootings and the old DOS days started me thinking about the batch ways of yesteryear. The need to reboot regarding the Market and the sleep issue is mute now. SoftLocker has resolved that issue.
I tried loading up SDK and did get past the path issue when using Win7 64 bit but the issued showed up again because my Packages To Install was empty...! So I put it aside.
Well I just fired up SDK again and low and behold, the Package list is now there. I guess Windoz just needed a cold boot...!
I'm new to Linux but have experience using low level and higher level programming languages so this is intriguing to me.
I'll investigate the SDK as I assume it is what's being used for writing apps.
I don't want to high jack your thread... maybe another thread could be started on the beginnings of app development. There might be enough people interested on this subject. Maybe move it out of this forum.
 
There's a development subforum over in the tcc8902 section.

Sent from my MID7015 using Tapatalk
 
So a script to load an app looks easy but is not.
From adb just run:
adb install /path/my.app.apk
Looks easy. Try that in the terminal
install /path/my.app.apk
and you get file not found, not the apk but the "install"
From xda-developers (I end up there a lot)
adb install in terminal emulator? - Page 2 - xda-developers
post #13 has a good script that works in this case.
The script though launches the installer which runs through the display of permissions and clicks required etc. Not good for a long list of apps.
So using a script for this doesn't look promising.

Another note is that I see a sh script is nothing more than a text file. I'm used to bash scripts (Bourne again Shell) which begin with #!/bin/bash. The sh scripts do not need a header line, just a simple text file like the old batch files in MS-DOS 3. I didn't test to see if the .sh is critical like the .bat was.
 
For Linux fans here is a bash script to use adb to install all the apps in a directory.
For example if you have backed up all the keeper apps and reinstall update.zip this script will reinstall all those keeper apps for you.
#! /bin/bash
#
# Android_Install_Directory
# by Ridgeland for AndroidTablets.net

echo
echo "# # # You should already be at the source directory # # #"
echo "for example:"
echo "/Data/Linux/OperatingSystems/Android/apps_apk/Puzzles"
echo "The apps (*.apk) files in the current directory will be installed on the Coby"
echo
pwd
echo

YesNo="Y";echo "Correct Directory - Continue? Y/n ";read YesNo
if [[ $YesNo == "Y" || $YesNo == "y" || $YesNo == "" ]]; then
echo "Running adb install *.apk"
else
echo "Exiting Program per user request ..."
exit
fi

# Make sure the server is running
adb devices

# process those files
for file in `ls *.apk` ; do
echo "Installing " $file
adb install $file
done

echo; echo "Installs Completed"; echo
 
Script for grey to move eBooks from /nand/ to /sdcard/
in Gscript: [x] Needs SU?
cp -r /nand/eBooks/* /sdcard/eBooks/
rm -r /nand/eBooks/*
cp copies the eBooks which are directories not just files.
rm removed the eBooks from /nand/
 
Script to edit lcd_density
This is a script I use in a Linux Terminal to edit lcd_density. What it does:
Make a backup of the original build.prop on the Coby
Copy build.prop to PC's /tmp/
Ask the user to choose 240, 220, 200, 180 or 160 (defaults to 200)
Edit PC's /tmp/build.prop per user's choice
Copy edited build.prop back to the Coby.
Reboot the Coby so the change takes effect.
#! /bin/bash
# Android/lcd_density.sh
# written by user Ridgeland for AndroidTablets
# March 30, 2011

echo; echo "Checking for tablet"
adb devices | tee /tmp/hold.txt
# if the tablet is there it will show up as device 0123456789ABCDEF
cat /tmp/hold.txt | grep 012345 > /tmp/flag.0
# if tablet is there /tmp/flag.0 file size is non-zero "-s" tests for file with size > 0
if [ -s /tmp/flag.0 ]; then
echo "Found Tablet"
else
echo "Tablet Not Found"; echo "Program aborting....."; echo
exit
fi

# Program will copy build.prop to the PC for modifications
# 2> /tmp/KB_s throws away the KB/s message so user doesn't see it
adb pull /system/build.prop /tmp/build.prop 2> /tmp/KB_s

echo; echo "Checking for copy of original build.prop"
# it's nice to have a copy of the original build.prop and not just last and current
# First create a file of size 0 (or chop existing file to 0)
: > /tmp/build.prop.original
adb pull /system/build.prop.original /tmp/build.prop.original 2>> /tmp/KB_s
if [ -s /tmp/build.prop.original ]; then
echo "build.prop.original already exists"
else
echo "Backup of original build.prop being created on the Tablet..."
adb push /tmp/build.prop /system/build.prop.original 2>> /tmp/KB_s
fi

# make a backup of the current build.prop
echo; echo "Backup copy of current build.prop being created on the Tablet..."
adb push /tmp/build.prop /system/build.prop.bak 2>> /tmp/KB_s

### the above was generic --- now on to this program's work

# Show user current setting for lcd_density
cat /tmp/build.prop | grep lcd_density > /tmp/hold.lcd.txt
CurrentLCD=`cat /tmp/hold.lcd.txt`
echo; echo "Currently lcd density set as"; echo $CurrentLCD; echo
# this could be done with a single line but it would be harder for a user to understand and modify.

# Ask user which lcd density to use
UserChoice=""
echo -n "Which lcd_density would you like 240, 220, 200, 180 or 160 (Defaults to 200): ";read UserChoice
if [[ $UserChoice == "" ]]; then
UserChoice="200"
fi
if [[ $UserChoice == "240" || $UserChoice == "220" || $UserChoice == "200" || $UserChoice == "180" || $UserChoice == "160" ]]; then
echo "Setting LCD Density to "$UserChoice
else
echo; echo "YO! Crash and Burn... due to PICNIC"
echo "Problem In Chair Not In Computer";
echo "I gave you five choices, even a default, and you screwed it up!"
echo $UserChoice" for crying out loud!";
echo "Program aborted. Duh..."; echo
exit
fi

# Replace update lcd_density per the user's choice
cat /tmp/build.prop | sed '/lcd_density/c\ro.sf.lcd_density='$UserChoice > /tmp/build.prop.new

YesNo="Y";echo -n "Ready to copy in the new setting and reboot the Coby? Y/n ";read YesNo
if [[ $YesNo == "Y" || $YesNo == "y" || $YesNo == "" ]]; then
adb push /tmp/build.prop.new /system/build.prop 2>> /tmp/KB_s
adb reboot
echo; echo "Coby tablet should be rebooting now..."
echo "Program completed, closing."; echo
else
echo; echo "All that and you punted! Oh well..."
echo "Program aborted."; echo
exit
fi
 
Script to edit heapsize
I wrote a script to edit lcd_density. It was a trival change to clone it for heapsize changes.
What it does:
Make a backup of the original build.prop on the Coby
Copy build.prop to PC's /tmp/
Ask the user to choose 16, 24, 32, 40 or 48 (Defaults to 24)
Edit PC's /tmp/build.prop per user's choice
Copy edited build.prop back to the Coby.
Reboot the Coby so the change takes effect.
#! /bin/bash
# Android/heapsize.sh
# written by user Ridgeland for AndroidTablets
# March 30, 2011

echo; echo "Checking for tablet"
adb devices | tee /tmp/hold.txt
# if the tablet is there it will show up as device 0123456789ABCDEF
cat /tmp/hold.txt | grep 012345 > /tmp/flag.0
# if tablet is there /tmp/flag.0 file size is non-zero "-s" tests for file with size > 0
if [ -s /tmp/flag.0 ]; then
echo "Found Tablet"
else
echo "Tablet Not Found"; echo "Program aborting....."; echo
exit
fi

# Program will copy build.prop to the PC for modifications
# 2> /tmp/KB_s throws away the KB/s message so user doesn't see it
adb pull /system/build.prop /tmp/build.prop 2> /tmp/KB_s

echo; echo "Checking for copy of original build.prop"
# it's nice to have a copy of the original build.prop and not just last and current
# First create a file of size 0 (or chop existing file to 0)
: > /tmp/build.prop.original
adb pull /system/build.prop.original /tmp/build.prop.original 2>> /tmp/KB_s
if [ -s /tmp/build.prop.original ]; then
echo "build.prop.original already exists"
else
echo "Backup of original build.prop being created on the Tablet..."
adb push /tmp/build.prop /system/build.prop.original 2>> /tmp/KB_s
fi

# make a backup of the current build.prop
echo; echo "Backup copy of current build.prop being created on the Tablet..."
adb push /tmp/build.prop /system/build.prop.bak 2>> /tmp/KB_s

### the above was generic --- now on to this program's work

# Show user current setting for heapsize
cat /tmp/build.prop | grep heapsize > /tmp/hold.heap.txt
CurrentHeap=`cat /tmp/hold.heap.txt`
echo; echo "Current heap size setting: "; echo $CurrentHeap; echo
# this could be done with a single line but it would be harder for a user to understand and modify.

# Ask user which heapsize to use
UserChoice=""
echo -n "Which heap size would you like 16, 24, 32, 40 or 48 (Defaults to 24): ";read UserChoice
if [[ $UserChoice == "" ]]; then
UserChoice="24"
fi
if [[ $UserChoice == "16" || $UserChoice == "24" || $UserChoice == "32" || $UserChoice == "40" || $UserChoice == "48" ]]; then
echo "Setting Heap Size to "$UserChoice
else
echo; echo "YO! Crash and Burn... due to PICNIC"
echo "Problem In Chair Not In Computer";
echo "I gave you five choices, even a default, and you screwed it up!"
echo $UserChoice" for crying out loud!";
echo "Program aborted. Duh..."; echo
exit
fi

# Replace update lcd_density per the user's choice
cat /tmp/build.prop | sed '/dalvik.vm.heapsize/c\dalvik.vm.heapsize = '$UserChoice'm' > /tmp/build.prop.new

YesNo="Y"; echo; echo -n "Ready to copy in the new setting and reboot the Coby? Y/n ";read YesNo
if [[ $YesNo == "Y" || $YesNo == "y" || $YesNo == "" ]]; then
adb push /tmp/build.prop.new /system/build.prop 2>> /tmp/KB_s
adb reboot
echo; echo "Coby tablet should be rebooting now..."
echo "Program completed, closing."; echo
else
echo; echo "All that and you punted! Oh well..."
echo "Program aborted."; echo
exit
fi
 
Backup/Restore AngryBirds high scores

The obvious problem with trying all these ROMs and/or starting over with a fresh install of the Coby ROM is that I've had to start over all the levels of AngryBirds a few times. Some levels took me like 30 tries (i.e. 3-2).

The fix is to save/restore two files from:
/data/data/com.rovio.angrybirds/files/
You'll need a rooted tablet to copy from and to /data/data/

In the new ROM you have to first create a highscores.lua by completing the first level 1-1. Then the restore script will work.

First is the script to backup your current high score. Just read it and you'll see what it does and how.
Run it with gscript with su power.

Both of these scripts assume you have three Angry Birds versions:
Angry Birds, Angry Birds - Seasons, Angry Birds - Rio
If you have more or less you'll need to edit the script for your use.

Copy and paste this to /sdcard/gscript/Games_Save_Scores.sh
# Android script
# Games_Save_Scores.sh
# May 2, 2011

echo; echo "SAVE Games"

Package1="com.rovio.angrybirds"
GameName1="AngryBirds"
Scores1a="highscores.lua"
Scores1b="settings.lua"

Package2="com.rovio.angrybirdsseasons"
GameName2="AngryBirdsSeasons"
Scores2a="highscores.lua"
Scores2b="settings.lua"

Package3="com.rovio.angrybirdsrio"
GameName3="AngryBirdsRio"
Scores3a="highscores.lua"
Scores3b="settings.lua"

# I cannot get if-statements to work with the Coby with 2.1!!!
# So this hack of always trying to make directories rather than just "if not there"

echo "Making directory /sdcard/gscript/ if needed"
mkdir /sdcard/gscript 2> /sdcard/delete.me.please
echo "Making directory /sdcard/gscript/Games/ if needed"
mkdir /sdcard/gscript/Games 2> /sdcard/delete.me.please
echo "Making directory /sdcard/gscript/Games/$GameName1/ if needed"
mkdir /sdcard/gscript/Games/$GameName1 2> /sdcard/delete.me.please
echo "Making directory /sdcard/gscript/Games/$GameName2/ if needed"
mkdir /sdcard/gscript/Games/$GameName2 2> /sdcard/delete.me.please
echo "Making directory /sdcard/gscript/Games/$GameName3/ if needed"
mkdir /sdcard/gscript/Games/$GameName3 2> /sdcard/delete.me.please
echo "Making directory /sdcard/gscript/Games/$GameName4/ if needed"
mkdir /sdcard/gscript/Games/$GameName4 2> /sdcard/delete.me.please

# # # AngryBirds # # #
echo "You get one safety to protect yourself from errrors."
echo "If an old $Scores1 exits it will be rename $Scores1.old"
echo
mv /sdcard/gscript/Games/$GameName1/$Scores1a /sdcard/gscript/Games/$GameName1/$Scores1a.old 2> /sdcard/delete.me.please
mv /sdcard/gscript/Games/$GameName1/$Scores1b /sdcard/gscript/Games/$GameName1/$Scores1b.old 2> /sdcard/delete.me.please
echo "Copying high scores file"
echo " from /data/data/$Package1/files/$Scores1"
echo " to /sdcard/gscript/Games/$GameName1/$Scores1"
cp /data/data/$Package1/files/$Scores1a /sdcard/gscript/Games/$GameName1/$Scores1a 2> /sdcard/delete.me.please
cp /data/data/$Package1/files/$Scores1b /sdcard/gscript/Games/$GameName1/$Scores1b 2> /sdcard/delete.me.please

# # # AngryBirdsSeasons # # #
echo "You get one safety to protect yourself from errrors."
echo "If an old $Scores2a exits it will be rename $Scores2a.old"
echo "If an old $Scores2b exits it will be rename $Scores2b.old"
echo
mv /sdcard/gscript/Games/$GameName2/$Scores2a /sdcard/gscript/Games/$GameName2/$Scores2a.old 2> /sdcard/delete.me.please
mv /sdcard/gscript/Games/$GameName2/$Scores2b /sdcard/gscript/Games/$GameName2/$Scores2b.old 2> /sdcard/delete.me.please
echo "Copying high scores file"
echo " from /data/data/$Package2/files/$Scores2a"
echo " to /sdcard/gscript/Games/$GameName2/$Scores2a"
echo " from /data/data/$Package2/files/$Scores2b"
echo " to /sdcard/gscript/Games/$GameName2/$Scores2b"
cp /data/data/$Package2/files/$Scores2a /sdcard/gscript/Games/$GameName2/$Scores2a 2> /sdcard/delete.me.please
cp /data/data/$Package2/files/$Scores2b /sdcard/gscript/Games/$GameName2/$Scores2b 2> /sdcard/delete.me.please

# # # AngryBirdsRio # # #
echo "You get one safety to protect yourself from errrors."
echo "If an old $Scores3 exits it will be rename $Scores3.old"
echo
mv /sdcard/gscript/Games/$GameName3/$Scores3a /sdcard/gscript/Games/$GameName3/$Scores3a.old 2> /sdcard/delete.me.please
mv /sdcard/gscript/Games/$GameName3/$Scores3b /sdcard/gscript/Games/$GameName3/$Scores3b.old 2> /sdcard/delete.me.please
echo "Copying high scores file"
echo " from /data/data/$Package3/files/$Scores3"
echo " to /sdcard/gscript/Games/$GameName3/$Scores3"
cp /data/data/$Package3/files/$Scores3a /sdcard/gscript/Games/$GameName3/$Scores3a 2> /sdcard/delete.me.please
cp /data/data/$Package3/files/$Scores3b /sdcard/gscript/Games/$GameName3/$Scores3b 2> /sdcard/delete.me.please

rm /sdcard/delete.me.please

echo "All done!"; echo
echo; echo "Games SAVED"
echo; echo "Games SAVED"
echo; echo "Games SAVED"; echo
sleep 4
The second script copies in a previous high score. Remember to first complete level 1-1 and then close AngryBirds.

Copy and paste this to /sdcard/gscript/Games_Restore_Scores.sh
## Android script
# Games_Restore_Scores.sh
# May 2, 2011

echo; echo "RESTORE Games"

Package1="com.rovio.angrybirds"
GameName1="AngryBirds"
Scores1a="highscores.lua"
Scores1b="settings.lua"
Package2="com.rovio.angrybirdsseasons"
GameName2="AngryBirdsSeasons"
Scores2a="highscores.lua"
Scores2b="settings.lua"
Package3="com.rovio.angrybirdsrio"
GameName3="AngryBirdsRio"
Scores3a="highscores.lua"
Scores3b="settings.lua"

echo "Hope you don't have the games open while this is running..."

cp /sdcard/gscript/Games/$GameName1/$Scores1a /data/data/$Package1/files/$Scores1a 2> /sdcard/delete.me.please
cp /sdcard/gscript/Games/$GameName1/$Scores1b /data/data/$Package1/files/$Scores1b 2> /sdcard/delete.me.please

cp /sdcard/gscript/Games/$GameName2/$Scores2a /data/data/$Package2/files/$Scores2a 2> /sdcard/delete.me.please
cp /sdcard/gscript/Games/$GameName2/$Scores2b /data/data/$Package2/files/$Scores2b 2> /sdcard/delete.me.please

cp /sdcard/gscript/Games/$GameName3/$Scores3a /data/data/$Package3/files/$Scores3a 2> /sdcard/delete.me.please
cp /sdcard/gscript/Games/$GameName3/$Scores3b /data/data/$Package3/files/$Scores3b 2> /sdcard/delete.me.please

echo
echo "AngryBirds High Scores"
ls -l /data/data/com.rovio.angrybirds/files
Owner=`ls -l /data/data/com.rovio.angrybirds/ | awk '{if (NR==1){print $3}}'`
echo "Owner = "$Owner
chown $Owner /data/data/com.rovio.angrybirds/files/highscores.lua 2> /sdcard/delete.me.please
chown $Owner /data/data/com.rovio.angrybirds/files/settings.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirds/files/highscores.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirds/files/settings.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirds/files/highscores.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirds/files/settings.lua 2> /sdcard/delete.me.please
ls -l /data/data/com.rovio.angrybirds/files

echo
echo "AngryBirdsSeasons High Scores"
Owner=`ls -l /data/data/com.rovio.angrybirdsseasons/ | awk '{if (NR==1){print $3}}'`
echo "Owner = "$Owner
chown $Owner /data/data/com.rovio.angrybirdsseasons/files/highscores.lua 2> /sdcard/delete.me.please
chown $Owner /data/data/com.rovio.angrybirdsseasons/files/settings.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirdsseasons/files/highscores.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirdsseasons/files/settings.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirdsseasons/files/highscores.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirdsseasons/files/settings.lua 2> /sdcard/delete.me.please
ls -l /data/data/com.rovio.angrybirds/files

echo
echo "AngryBirdsRio High Scores"
ls -l /data/data/com.rovio.angrybirdsrio/files
Owner=`ls -l /data/data/com.rovio.angrybirdsrio/ | awk '{if (NR==1){print $3}}'`
echo "Owner = "$Owner
chown $Owner /data/data/com.rovio.angrybirdsrio/files/highscores.lua 2> /sdcard/delete.me.please
chown $Owner /data/data/com.rovio.angrybirdsrio/files/settings.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirdsrio/files/highscores.lua 2> /sdcard/delete.me.please
chgrp $Owner /data/data/com.rovio.angrybirdsrio/files/settings.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirdsrio/files/highscores.lua 2> /sdcard/delete.me.please
chmod 666 /data/data/com.rovio.angrybirdsrio/files/settings.lua 2> /sdcard/delete.me.please
ls -l /data/data/com.rovio.angrybirdsrio/files

echo "All done!"; echo
echo; echo "Games RESTORED"
echo; echo "Games RESTORED"
echo; echo "Games RESTORED"; echo
sleep 4
Note the last section is to change the owner of the files. If owner is not correct when AngryBirds tries to write a new score the app crashes. On my tablet my script also backs up Jewels. A little experimenting and you can see what to back up for other games.
 
Last edited:
Back
Top