Post Reply 
[Tutorial] Bash Scripting
09-08-2011, 01:03 PM (This post was last modified: 09-09-2011 01:47 PM by Wolf95.)
Post: #1
[Tutorial] Bash Scripting
I've been Bash Scripting for quite some time now and being that it is VERY useful for VPSs, I figure it may be helpful for some of you, so here it goes:
BASH (Bourne Again SHell) is the default shell for distributions such as Debian, SuSE uses SH, which in my perspective isn't as feature rich, but if you have both installed you can toggle between shells inside of your script by using the "SH-bang" which is like this for BASH
Code:
#!/bin/bash
or if you have SH
Code:
#!/bin/sh
I am used to programming in BASH so, some of this may not work for SH.

Displaying input:
echo - Prints Text/Variables to screen, often use quotation marks so it looks like this:
Code:
#!/bin/bash
echo "hello this is text that will display on screen"
the result will be:
Code:
hello this is text that will display on screen
you may also use print, which does almost the exact thing, but in some cases it can allow more complex interaction between programs. Such as having automation with screen, more of that later on.

Variables:
Variables - Do not have to be registered to be used, you can register a variable like this:
Code:
#!/bin/bash
variable="hello"
echo $variable
Output:
Code:
hello

IF this... Then Do.... Else...
If statements are pretty easy in bash, they go like this:
Code:
#!/bin/bash
if [ $variable == y ]; then echo "This is the conditional for yes"
else echo "unkown input"
fi
the fi tells the shell that that if-statement is done, so that other else statements will apply to that section of coding.

Reading user input
read variable - This will take the input from the user and store it in $variable
Code:
#!/bin/bash
echo "Enter Text"
read text
echo "you entered $text"

Performing Commands
You can have your shell script perform commands, here i will demonstrate a minecraft server installer.
Code:
#!/bin/bash
echo "Preparing to install Required Programs."
apt-get update -q
echo "Done Preparing, Fetching and installing..."
apt-get install openjdk-6-jre screen -q
echo "done"

Assigning Variables to Commands:
You can assign variables to commands using backticks (`)
Code:
#!/bin/bash
variable=`pwd`
echo "Working directory is $variable"
This will echo your current directory.

I'll add more to this later, maybe.
Added 9/8/2011=======================

This next part becomes more difficult, basically it pipes output from a program and selects a part of it to perform a action with. For Example: I will display the amount of free Ram on the System.
Code:
#!/bin/bash
$freeram=`free -t -m | egrep Mem | awk '{print $4}'`
$totalram=`free -t -m | egrep Mem | awk '{print $2}'`
$usedram=`free -t -m | egrep Mem | awk '{print $3}'`
echo ${usedram}MB/${totalram}MB Used....
How does it work?
grep and awk work together to select text, if you have played around in linux you know that the unprocessed output of free -t -m is:
Code:
root@debian:~# free -t -m
             total       used       free     shared    buffers     cached
Mem:          2026       1208        818          0          5        685
-/+ buffers/cache:        517       1509
Swap:            0          0          0
Total:        2026       1208        818
root@debian:~#
Note: | separates the different text modifiers(I forget technical name) so: egrep selects the row, then awk select the block of text so after egrep this is what it sees:
Code:
Mem:          2026       1208        818          0          5        685
then
notice the second section using awk:
$freeram=`free -t -m | egrep Mem | awk '{print $4}'`
this will select the fourth block of text, in this case, 818.

Alright, taking a little detour from that we will now take a look at loops,
Loops - Allow you to perform actions such as parsing files or, cycling through variables, files, etc. I know you see no purpose in the example, but it pretty much gets the concept down:
Code:
#!/bin/bash
#This is a note, I forgot to add it above.
#set initial variable
increment=1
while (( increment < 7 ))
do
echo "Increment: $increment"
(( increment++ ))
done
There are other ways of doing this, and I am not that good with BASH loops so I will avoid those for now, but this script sets the increment counter to 1, and then as long as increment is less than 7 it will say: "Increment #" and the (( increment++ )) will add 1 to it, also know as "Incrementing"


There is an advanced way of performing if commands, This is usually used in init scripts/Programs with menus, In this example I will also combine how to handle input variables as well.

Note: Each Input variable is handled as $1.. $2... etc. so for this:

script.sh hello nosql

$1 would equal hello and $2 would equal no sql

Case basically a long if-statement


Code:
#!/bin/bash
case $1 in
1) echo Input is one
echo see this is line 2
echo second parameter is $2
;;
*) echo I'm a wildcard
;;
esac
Notice how there are double semi colons ;;
Those end that code block, and the esac ends the case function.
User Tools
Quote this message in a reply


09-08-2011, 01:32 PM
Post: #2
RE: [Tutorial] Bash Scripting
Nice tutorial on bash. I need to learn it. Confused.
User Tools
Quote this message in a reply
09-08-2011, 04:44 PM
Post: #3
RE: [Tutorial] Bash Scripting
Thank for nice tutorial Bash Scripting. I also need to learn about it too

Millions Thank FreeVPS & LoomHosts for my lovely VPS8
User Tools
Quote this message in a reply
09-08-2011, 05:18 PM
Post: #4
RE: [Tutorial] Bash Scripting
nice introduction of bash script.
its look like php right?
User Tools
Quote this message in a reply
09-09-2011, 05:16 AM
Post: #5
RE: [Tutorial] Bash Scripting
Good intro to BASH scripting Smile

Please add more !

.
.
Thank you freeVps and Loomhosts for the amazing VPS
.
.
User Tools
Quote this message in a reply
09-09-2011, 01:48 PM
Post: #6
RE: [Tutorial] Bash Scripting
Thanks for the positive feedback, I've added more, and it is very similar to PHP, that's why I think I caught on to PHP so easily.
User Tools
Quote this message in a reply
09-09-2011, 11:21 PM
Post: #7
RE: [Tutorial] Bash Scripting
Nice tutorial.

Don't worry about the world coming to an end today. It's already tomorrow in Australia.
User Tools
Quote this message in a reply
09-16-2011, 03:45 AM
Post: #8
RE: [Tutorial] Bash Scripting
Hello,Thanks!!This is really a good tutorial!
I learn a bit from the bash language,and thank you,i wil find the bash language and learn!

Thanks Carstensz Pyramid Server and FreeVPS very much my VPS18.
User Tools
Quote this message in a reply
Post Reply 


Forum Jump:



User(s) browsing this thread:
1 Guest(s)