[Android General] Paget96:
Hello guys, I want to share my expirience with users who wants to learn basic scripting stuff, busybox commands, how to use them, how to execute them, how to make .sh files.

Ok, let's start!!! :D

​

First we must to learn what is busybox. BusyBox is software that provides several stripped-down Unix tools in a single executable file. It runs in a variety of POSIX environments such as Linux, Android.

Busybox allows you or programs to perform actions on your phone using Linux (copied from Unix) commands. Android is basically a specialized Linux OS with a Java compatible (Dalvik) machine for running programs. The Android kernel is a modified version of the Linux kernel (that is why the Android kernel must always be open source). Busybox gives functionality to your phone that it does not have without it. Many programs, especially root programs such as Titanium Backup, require busybox to perform the functions of the program. Without busybox installed your phone is much more limited in what it can do.

Here is some info about busybox, you can track here release dates, you can download sources...

http://spblinux.de/2.0/doc/commands.html

Also here is busybox installer for our "linux devices" heheh ^~^ by stericson. You have two versions of it as you see paid and free. :D

Chose paid if you want to donate him for awesome job.

Free:

https://play.google.com/store/apps/details?id=stericson.busybox

Paid(PRO):

https://play.google.com/store/apps/details?id=stericson.busybox.donate

After that you need Terminal app, you can download it from playstore too.

Link:

Download it from here :)

This app emulates Linux terminal, with it you can access to android shell.

Lets's start with learning basic terminal stuff

The most of shell commands doesn't need root access, but some of them are need it.

To give root access to the terminal app, you need to type next.

su [I](press enter)[/I]

After pressing enter, another window will popup to grant root access. Tap on GRANT

***When you typing something into the terminal window, take care because EVERYTHING IS CASE SENSITIVE.***

We all know there are three permissions per group on a file read,write,execute or rwx.

To set permissions as rw-r--r-- we use chmod 644 or 0644 but most don't know why ? Here's why

Read or r has a positional weight of 4, Write or w has 2 and Execute or x has 1. Add them up for each group and you'll get the chmod number to use. For above example, the numbers will be (4+2)(4)(4) i.e. 644 or if you want four digits, use 0644 and that's pretty much it. It comes in very handy if you're learning shell scripting because Linux revolves around permissions. (Thanks to [MENTION=6552280]MSF Jarvis[/MENTION])

Let's go trough basic shell commands. :D :D :D​

If you interested about all commands, you can check them

HERE

.

cat [file destination] - use this command to print everything from a file to the terminal window

Example:

Open terminal window, and type next:

busybox cat /proc/meminfo [I](press enter)[/I]

You will get output after pressing enter :D

Now, you can see here, if you open any file manager and go to destination which was given for example "/proc/meminfo"

You will see next file and stuff inside it :)

Heheh, is it same?? ^~^ for sure that it is, you can use it for any file on your device which have text in it.

Let's go to the next basic command ^~^

echo - this command can be used for printing arguments to stdout. Also you can use it for adding text in already existing file which we need here, because we are learning scripting stuff, right? ^~^

Example:

Open terminal window, and type next:

busybox echo "Scripting is awesome" > /sdcard/test [I](press enter)[/I]

Ok, let's explain this.

First you added a command echo to add something in existing file, then between "..." we need to put arguments, for our case this is "Scripting is awesome" next we see symbol " > " which will forward arguments to next added direction, in our case this is "/sdcard/test".

First we need to have a existing file (IMPORTANT)

After we agreed with that we have a file

Open terminal as I already explained and after pressing enter you will get output on the terminal window.

That is a file from our /sdcard/

Don't delete this file, we will use it later with " sh" command.

Let's go to the next basic command :D

clear - Use this comand to clear whole terminal window.

Example:

You have a terminal window which have a lot of stuff on it, for example stuff from previous lines, text, etc.

You can simply make it blank with just one command

busybox clear [I](then press enter)[/I]

And voila, terminal window is blank ;)

touch [file destination] - Use this command to create a blank file on any destination on your android device.

Example:

Open terminal window, and type next:

busybox touch /sdcard/scripting_007 [I](press enter)[/I]

This will create file called "scripting_007" on "/sdcard/" in this case, you can put any.

As you can see file "scripting_007" exist on /sdcard/

mkdir [file destination] - Use this command to create one empty folder on any destination on your android device.

Example:

Open terminal window, and type next:

busybox mkdir /sdcard/development [I](press enter)[/I]

This will create folder called "development" on "/sdcard/" in this case, you can put any.

sh [path of a file] - Use this command to execute basic script

Example:

Open terminal window, and type next:

sh /sdcard/test [I](press enter)[/I]

This will execute file which you make before with "echo" command :D

sleep [time in secs] - Use this command to make an offset between executing lines inside script.

Example:

Open terminal window, and type next:

busybox echo "Scripting is"
busybox sleep 10
busybox echo "awesome"

This is a combination of two commands as you can see, echo and sleep.

After launching this simple script in terminal window you will get first "Scripting is" and after 10 secs you will get awesome. :)

rm [ -f (file) -rf (folder)] - this is a command for removing files (-f) and folders (-rf)

Example:

Create a file called "android" on sdcard, then open terminal window, and type next:

busybox rm -f /sdcard/android

This simple code will remove a file called android from sdcard. :)

Now do the same thing with a folder, create one with the name "android" and run terminal emulator

busybox rm -rf /sdcard/android

This will remove folder called android from your sdcard.

Now, let's go with some advanced stuff​

You need any text editor, I suggesting this one

Link:

Download from here :)

Now, open the app and write a new file and give it a name "test1"

Inside a file write next text:

busybox echo "Android"
busybox sleep 2
busybox echo "is"
busybox sleep 2
busybox echo "Awesome"

Save the file and copy it to some location, i suggest /sdcard/.

Open up terminal emulator and type next

sh /sdcard/test1 [I](press enter)[/I]

This thing will execute your script with the name test1 from /sdcard/.

Output will be "Android", then after two seconds will appear "is" and then after two more seconds "Awesome" will appear. THATS IT!!! You learned how to make a file (script) and how to execute it with terminal emulator. :D

Now do the same thing make a file with the name "test2" and make another file with a name "print"

First open "test1" and write next:

busybox echo "After 5 seconds"
busybox sleep 1
busybox echo "You will get a suprice"
busybox sleep 2
busybox clear
busybox echo "1"
busybox sleep 1
busybox clear
busubox echo "2"
busybox sleep 1
busybox clear
busybox echo "3"
busybox sleep 1
busybox clear
busybox echo "4"
busybox sleep 1
busybox clear
busybox echo "5"
busybox sleep 1
busybox clear
sh /sdcard/print

I think that you already know whats happened here :D

You have print, then you have sleep to make offset, and after that you have clear command to make window blank :D

Now open file with the name "print"

Inside it write next:

busybox echo "Voila, suprise"

Save this file, and open terminal emulator, and write next:

sh /sdcard/test2

And magic happens :D

In this example you learned how to execute second file with first, I think that it's not difficult for you :D

In this example we will play a bit with colors outputs :D

Codes for colors are below

busybox echo -e " 1. Black \e[0;100m \e[00;37;40m"
busybox echo -e " 2. Red \e[0;101m \e[00;37;40m"
busybox echo -e " 3. Green \e[0;102m \e[00;37;40m"
busybox echo -e " 4. Yellow \e[0;103m \e[00;37;40m"
busybox echo -e " 5. Blue \e[0;104m \e[00;37;40m"
busybox echo -e " 6. Purple \e[0;105m \e[00;37;40m"
busybox echo -e " 7. Cyan \e[0;106m \e[00;37;40m"
busybox echo -e " 8. White \e[0;107m \e[00;37;40m"

Try to copy this into terminal window and see what will happened or try whing which you learned before with the executing files. Copy whole code to a file and execute it with "sh" command. :D USE YOUR KNOWLEDGE. :D

Here you can see all things based on bash script coloring :D

https://wiki.archlinux.org/index.php/Color_Bash_Prompt

or

http://misc.flogisoft.com/bash/tip_colors_and_formatting

Here you can see all codes and it's nice descirbed. :D

Now, let's start :)

Make a file with the name "test3" open it and write or copy next code inside:

busybox echo " .... "
busybox echo " / / "
busybox echo " / / "
busybox echo " / / "
busybox echo " / /_._._. "
busybox echo -e " /_._._._._./ \e[00;41m SPEED MOD \e[00;37;40m v3.0"
busybox echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
busybox echo -e "\e[01;32m Feel smoothness \e[00;37m"
busybox echo " "

As you can see this part was taken from mine L Speed performance/battery mod :D

Save the file, open terminal and type inside

sh /sdcard/test3

And you will get the same screen like in first L Speed menu.

Let's play a bit with this :)

You see the code

\e[00;41m SPEED MOD \e[00;37;40m v3.0

Just change one number instead of 41 write 42 and you will get green color, then give a try to other nubers. Every number is another color for example:

# Reset
Color_Off='\e[0m' # Text Reset

# Regular Colors
Black='\e[0;30m' # Black
Red='\e[0;31m' # Red
Green='\e[0;32m' # Green
Yellow='\e[0;33m' # Yellow
Blue='\e[0;34m' # Blue
Purple='\e[0;35m' # Purple
Cyan='\e[0;36m' # Cyan
White='\e[0;37m' # White

# Bold
BBlack='\e[1;30m' # Black
BRed='\e[1;31m' # Red
BGreen='\e[1;32m' # Green
BYellow='\e[1;33m' # Yellow
BBlue='\e[1;34m' # Blue
BPurple='\e[1;35m' # Purple
BCyan='\e[1;36m' # Cyan
BWhite='\e[1;37m' # White

# Underline
UBlack='\e[4;30m' # Black
URed='\e[4;31m' # Red
UGreen='\e[4;32m' # Green
UYellow='\e[4;33m' # Yellow
UBlue='\e[4;34m' # Blue
UPurple='\e[4;35m' # Purple
UCyan='\e[4;36m' # Cyan
UWhite='\e[4;37m' # White

# Background
On_Black='\e[40m' # Black
On_Red='\e[41m' # Red
On_Green='\e[42m' # Green
On_Yellow='\e[43m' # Yellow
On_Blue='\e[44m' # Blue
On_Purple='\e[45m' # Purple
On_Cyan='\e[46m' # Cyan
On_White='\e[47m' # White

# High Intensity
IBlack='\e[0;90m' # Black
IRed='\e[0;91m' # Red
IGreen='\e[0;92m' # Green
IYellow='\e[0;93m' # Yellow
IBlue='\e[0;94m' # Blue
IPurple='\e[0;95m' # Purple
ICyan='\e[0;96m' # Cyan
IWhite='\e[0;97m' # White

# Bold High Intensity
BIBlack='\e[1;90m' # Black
BIRed='\e[1;91m' # Red
BIGreen='\e[1;92m' # Green
BIYellow='\e[1;93m' # Yellow
BIBlue='\e[1;94m' # Blue
BIPurple='\e[1;95m' # Purple
BICyan='\e[1;96m' # Cyan
BIWhite='\e[1;97m' # White

# High Intensity backgrounds
On_IBlack='\e[0;100m' # Black
On_IRed='\e[0;101m' # Red
On_IGreen='\e[0;102m' # Green
On_IYellow='\e[0;103m' # Yellow
On_IBlue='\e[0;104m' # Blue
On_IPurple='\e[0;105m' # Purple
On_ICyan='\e[0;106m' # Cyan
On_IWhite='\e[0;107m' # White

On the next phase we will learn how to use variables, in bash scripts this is simple, just add a simple "tag" then what you want to connext wit this tag.

Here is an example:

file=/sdcard/test4
busybox touch $file

Ok, here is one simple thing, this is same like in touch command explaining, but there is a little difference. We added variable or you can call it shortcut if it's easier for you.:)

To call variable somewhere in a file you need to use a dollar sign "$" then name of your variable. Let's to next examole with variables.

As we now have file called test4 on our SD, we can go trough. :)

file=/sdcard/test4
busybox echo "hey file" > $file

As you can see everything is repeating from previous examples just with variable. This is much easier, and you will save a bit of time. :)

Now we can see what is inside file :) Open terminal emulator and type next

busybox cat /sdcard/test4

And you will get output "hey file" :)

Simple isn't it? :D

In this part we will learn how to use if, elif, else :) It's pretty simple, let's start.

Pattern for this will be:

if [what]; then
do something
fi

if [what]; then
do something
elif [what if not "if"]
do something
else
what to do else
fi

Ok what we have here?

The first part of code show us simple code which mean if for example something is true then do something and then close if with fi.

Second part of code is a bit "complicated". If somethig is true then do something, then we have elif, in case that if is false then it will do what is under elif. In case that both of them are false, everything what is under else will be executed.

Ok here is an example:

if [ -e /sdcard/test5 ]; then
busybox echo "hello" > /sdcard/test5
else
busybox touch /sdcard/test5
busybox echo "hello" > /sdcard/test5
fi

Ok, if file test 5 exists add hello to it, else(if not) then create a file and add text hello in it. :)

Another example with a bit of complications ;) heheh :D

if [ -e /sdcard/test5 ]; then
busybox echo "file 5 exists"
elif [ -e /sdcard/test6 ]; then
busybox echo "file 6 exists"
else
busybox echo "files are not exist"
fi

Ok, here is explanation, if test5 exist then print "file 5 exists" going to the next line, if file test6 exist pri t "file 6 exists" and else if both of them doesn't exist print "files are not exist".

Donation:

http://xdaforums.com/donatetome.php?u=5514152