I've already used a bit of BASH scripting in my Wifi sniffing tutorial, but the importance of scripting in BASH and other languages such as Perl, Ruby, and Python is so great I need to write separate posts for them all.
Bash stands for "Bourne-Again Shell" (you will see "sh" stands for "shell" in many places). Named aptly for being the successor of the Bourne Shell, it came into use in 1989 and has since been a main scripting language for Linux and has many different options such as piping (seen before on my blog), variables and control structures (like all good languages), file reading, and the Unix "wildcard" usage by the asterisk (*) key.
Enough about stuff I'm sure you guys don't care about, lets jump right in!
First, I'll start off with some basic BASH variables and interesting things you should know before attempting to write your own program, then I'll go over basic programming syntax in a BASH environment and show some examples.
In Unix as well as MSDOS and Windows, there are variables (I'm sure we all know what variables are) called "environment" variables, and they deal with certain processes running and affect those processes in many ways. Here's a short list of important ones:
PATH - lists directories the shell searches, for the commands the user may type without having to provide the full path.
HOME - indicates where the user's "home" directory is located in the file hierarchy.
USER - indicates the current user. Try running the command "echo $USER" and viewing the output; it should be your login.
TERM - specifies the type of terminal being used by the user.
PS1 - specifies how the prompt is displayed in the shell or terminal while the system waits for a command. Mine on Backtrack5 is ">" but for some it is "$" this can be changed to be anything (for instance, your current directory for easy knowledge).
PS2 - specifies how the prompt is displayed in the shell or terminal while the system waits for more input; it is like the PS1, but instead of when there is no command running, this is for when a command or process is waiting for more input.
MAIL - used to indicate where a user's mail is to be found.
TEMP - location where processes can store temporary files while running a script or process.
More will be added, but this is a good list of some important ones you can use for now. Try Googling with the Google search above if you want to find out more about environment variables in Linux/Unix.
Now before starting your scripting career in BASH, there are some important things you need to remember. The first is that one should ALWAYS ALWAYS ALWAYS start their script file with #!/bin/bash (which you will hear programmers refer to as "hash bang").
This tells the Unix environment what type of shell you're using (bash in this case), and the location of the shell.
Have this set as your first line and keep a couple blank lines in between this and the actual code so you realize where this is. It should ALWAYS be first. I think you get the importance of this.
For future note when I write my Perl tutorial, the first line in a Perl (.pl) script must be "#!/usr/bin/perl"
When you make your script file, whether it be through Nano or another text editor such as Vi/Vim, I like to have the format in the format: "filename.sh" where the ".sh" tells us it's a shell script file. You can name it mostly anything, but it's quite a bit easier keeping it ".sh" so later if you're using the ls command or whatnot, you can search for all your shell scripts!
After we've created the file and added in the #!/bin/bash line, we need to make this executable by the system. To do this, we type the command "chmod +x filename.sh"
What this does is adds to the file the access of executable (the plus sign adds, and the x stands for executable). After we've done this, we can run the file by typing a few different commands.
You can either type "./filename.sh" or "sh filename.sh" or "bash filename.sh" to run it. The first requires you to be in the same directory, however.
Other options you can add are "r" and and "w" which stands for read and write respectively. You can add and remove these privileges by typing +rwx or -rwx depending on which you want. You can also use numbers to differentiate what privleges you want.
Instead of my reiterating this topic, here's an excellent and short website that explains it.
I'm sure most of you have programmed before, whether it be in C++, Java, or some other language and are somewhat aware of how to use if then statements, but for those who aren't, I'll explain what they are quickly and for everyone the syntax (which means formation) of them in BASH.
If then statements are pretty easy to understand since they're named aptly for their use. They are used as a conditional statement (meaning, it tests a condition) and depending on the return (whether or not it is true or false) it executes (or runs) a certain line or lines of code that you choose.
The syntax of if then statements in BASH is as follows:
if [condition]; then
expression if true
else
expression otherwise if false
fi # ends if statement
The ending "fi" is necessary for BASH to tell the computer where your if statement starts and ends. "fi" is of course "if" backwards.
You can have an if then statement with only one set of expressions (it doesn't require an "else" part), or as many "elses" as you want, but you HAVE TO end with the "fi" line.
Depending on how many elses you wish you add, there are many other ways to do this in an easier fashion that I will cover later, as well.
My favorite way to do for loops is this:
for var in {1..10}
do
echo "the variable var is $var"
done # closes for loop
If you're used to Java or C++ or another high level programming language and their for loops, you can use this syntax:
for (( i=0; i<10; i++ )) # note that spaces are a MUST (BASH is weird like this)
do
echo "increment variable i is $i"
done # closes for loop
You can add a "break" command inside the loop, which I would recommend throwing into an "if then" statement for error or input checking.
As well as the break command, you can have a "continue" command which automatically skips to the next iteration; meaning if i is equal to 5, but whatever you want has already been completed, you can have an if statement check your needs then simply add the "continue" statement and it will go to the 6th iteration.
This gets into bigger and better scripts in BASH and can be used quite effectively depending on your scripting needs.
Bash stands for "Bourne-Again Shell" (you will see "sh" stands for "shell" in many places). Named aptly for being the successor of the Bourne Shell, it came into use in 1989 and has since been a main scripting language for Linux and has many different options such as piping (seen before on my blog), variables and control structures (like all good languages), file reading, and the Unix "wildcard" usage by the asterisk (*) key.
Enough about stuff I'm sure you guys don't care about, lets jump right in!
First, I'll start off with some basic BASH variables and interesting things you should know before attempting to write your own program, then I'll go over basic programming syntax in a BASH environment and show some examples.
In Unix as well as MSDOS and Windows, there are variables (I'm sure we all know what variables are) called "environment" variables, and they deal with certain processes running and affect those processes in many ways. Here's a short list of important ones:
PATH - lists directories the shell searches, for the commands the user may type without having to provide the full path.
HOME - indicates where the user's "home" directory is located in the file hierarchy.
USER - indicates the current user. Try running the command "echo $USER" and viewing the output; it should be your login.
TERM - specifies the type of terminal being used by the user.
PS1 - specifies how the prompt is displayed in the shell or terminal while the system waits for a command. Mine on Backtrack5 is ">" but for some it is "$" this can be changed to be anything (for instance, your current directory for easy knowledge).
PS2 - specifies how the prompt is displayed in the shell or terminal while the system waits for more input; it is like the PS1, but instead of when there is no command running, this is for when a command or process is waiting for more input.
MAIL - used to indicate where a user's mail is to be found.
TEMP - location where processes can store temporary files while running a script or process.
More will be added, but this is a good list of some important ones you can use for now. Try Googling with the Google search above if you want to find out more about environment variables in Linux/Unix.
Now before starting your scripting career in BASH, there are some important things you need to remember. The first is that one should ALWAYS ALWAYS ALWAYS start their script file with #!/bin/bash (which you will hear programmers refer to as "hash bang").
This tells the Unix environment what type of shell you're using (bash in this case), and the location of the shell.
Have this set as your first line and keep a couple blank lines in between this and the actual code so you realize where this is. It should ALWAYS be first. I think you get the importance of this.
For future note when I write my Perl tutorial, the first line in a Perl (.pl) script must be "#!/usr/bin/perl"
When you make your script file, whether it be through Nano or another text editor such as Vi/Vim, I like to have the format in the format: "filename.sh" where the ".sh" tells us it's a shell script file. You can name it mostly anything, but it's quite a bit easier keeping it ".sh" so later if you're using the ls command or whatnot, you can search for all your shell scripts!
After we've created the file and added in the #!/bin/bash line, we need to make this executable by the system. To do this, we type the command "chmod +x filename.sh"
What this does is adds to the file the access of executable (the plus sign adds, and the x stands for executable). After we've done this, we can run the file by typing a few different commands.
You can either type "./filename.sh" or "sh filename.sh" or "bash filename.sh" to run it. The first requires you to be in the same directory, however.
Other options you can add are "r" and and "w" which stands for read and write respectively. You can add and remove these privileges by typing +rwx or -rwx depending on which you want. You can also use numbers to differentiate what privleges you want.
Instead of my reiterating this topic, here's an excellent and short website that explains it.
If->Then conditional statements:
I'm sure most of you have programmed before, whether it be in C++, Java, or some other language and are somewhat aware of how to use if then statements, but for those who aren't, I'll explain what they are quickly and for everyone the syntax (which means formation) of them in BASH.
If then statements are pretty easy to understand since they're named aptly for their use. They are used as a conditional statement (meaning, it tests a condition) and depending on the return (whether or not it is true or false) it executes (or runs) a certain line or lines of code that you choose.
The syntax of if then statements in BASH is as follows:
if [condition]; then
expression if true
else
expression otherwise if false
fi # ends if statement
The ending "fi" is necessary for BASH to tell the computer where your if statement starts and ends. "fi" is of course "if" backwards.
You can have an if then statement with only one set of expressions (it doesn't require an "else" part), or as many "elses" as you want, but you HAVE TO end with the "fi" line.
Depending on how many elses you wish you add, there are many other ways to do this in an easier fashion that I will cover later, as well.
For Loops:
Now again, if you've had any experience with programming, for loops shouldn't be anything new to you; however, for loops in BASH have a little different of a syntax. This time, I'm going to go over the syntax of a for loop in BASH, then explain the uses for our new readers and how these are one of the most important aspects of programming.
In BASH, there are a few ways to do for loops, which is interesting because in most programming languages there's one basic syntax. Here are a few ways to do them.My favorite way to do for loops is this:
for var in {1..10}
do
echo "the variable var is $var"
done # closes for loop
If you're used to Java or C++ or another high level programming language and their for loops, you can use this syntax:
for (( i=0; i<10; i++ )) # note that spaces are a MUST (BASH is weird like this)
do
echo "increment variable i is $i"
done # closes for loop
You can add a "break" command inside the loop, which I would recommend throwing into an "if then" statement for error or input checking.
As well as the break command, you can have a "continue" command which automatically skips to the next iteration; meaning if i is equal to 5, but whatever you want has already been completed, you can have an if statement check your needs then simply add the "continue" statement and it will go to the 6th iteration.
This gets into bigger and better scripts in BASH and can be used quite effectively depending on your scripting needs.
While Do Loops and Do Until Loops:
Another basic and important programming syntax to understand is Do->While loops, which can be either do while or do until. I'll explain both and their uses.
The basic while loop syntax is as follows:
while [ expression ]; do
<< Block >>
done
Note that the squared parenthesis must have spaces between the tested expression or it won't compile and run.
A do until loop does the block statement UNTIL the expression evaluates to true, which is the opposite of the while do loop.
Here is an example:
until [ expression ]; do
<< Block >>
done
So if you have the expression "i = 0" and the variable is prompted every time the loop goes through, if we used a while loop, the variable would only continue if the use input "0" every time, but if we used an "until," the loop would continue UNTIL i equals 0, meaning that ANY other number would continue the loop other than 0.
The basic while loop syntax is as follows:
while [ expression ]; do
<< Block >>
done
Note that the squared parenthesis must have spaces between the tested expression or it won't compile and run.
A do until loop does the block statement UNTIL the expression evaluates to true, which is the opposite of the while do loop.
Here is an example:
until [ expression ]; do
<< Block >>
done
So if you have the expression "i = 0" and the variable is prompted every time the loop goes through, if we used a while loop, the variable would only continue if the use input "0" every time, but if we used an "until," the loop would continue UNTIL i equals 0, meaning that ANY other number would continue the loop other than 0.
Gah, yer inspiring me to take up Python...plus I've heard it's easier than C++. Or was it PERL...?
ReplyDeleteWe'll see.
Python and Perl are scripting languages and C++ is more of an object based high level language... C++ is pretty easy to understand, though.
ReplyDeleteI'll hopefully be posting some C++, Python, and Perl stuff after my BASH tutorial, so look back for that!
If you're going to be learning your first language, I suggest Python. Definitely would not advise C++ as a first.
ReplyDeleteI want to start learning Python, but my networking class is consuming all of my time. We will be using the CLI on Windows and on routers though, but that is nothing like writing a program.
ReplyDeleteGreat guide on scripting, Thank you for this.
ReplyDeletegreat guide for Linux lovers. Thank you.
ReplyDelete2016-3-23 leilei
ReplyDeletemichael kors handbags
converse shoes
jimmy choo shoes
cheap oakley sunglasses
rolex replica watches
michael kors outlet
valentino store
fitflops clearance
ralph lauren outlet
longchamp
polo ralph lauren
christian louboutin shoes
ray ban sunglasses
coach outlet store online
armani jeans
stephen curry shoes
prada uk
ralph lauren outlet
christian louboutin uk
coach factory outlet
michael kors outlet
coach factory outlet
coach outlet online
gucci borse
cheap jordans
canada goose sale
louboutin
coach outlet online
ecco shoes
marc jacobs handbags
coach factory outlet
ralph lauren
ray ban sunglasses
coach outlet online
abercrombie & fitch
nike running shoes
michael kors outlet
hollister uk
babyliss flat iron
cheap jordan shoes
soccer jerseys
ReplyDeletetoms outlet
lululemon outlet
michael kors handbags clearance
ray ban sunglasses
air max 90
cartier outlet store
swarovski outlet
louis vuitton outlet store
oakley sunglasses
rolex watches
lebron shoes
michael kors factory outlet
oakley sunglasses
oakley sunglasses
michael kors outlet
hollister clothing store
mulberry outlet
cartier watches for sale
adidas outlet store
ralph lauren polo
rolex watches
true religion outlet
rolex watches for sale
longchamp handbags
mulberry handbags sale
timberland shoes
cazal outlet
omega watches
louis vuitton outlet
longchamp pas cher
cheap oakley sunglasses
michael kors outlet online
michael kors handbags
mulberry sale
chanyuan0523
chenlina20160625
ReplyDeletecoach outlet
lebron james shoes 13
asics outlet
kate spade handbags
longchamp bags
cheap toms
cheap air jordans
tory burch outlet online
adidas uk
michael kors outlet
jordan retro
ralph lauren outlet
coach outlet
true religion
louis vuitton outlet
adidas superstar
oakley sunglasses
pandora outlet
christian louboutin outlet
kate spade handbags
kids lebron shoes
coach outlet
coach factory outlet
cheap jordan shoes
michael kors outlet clearance
louboutin
ray bans
coach outlet store online
nfl jerseys wholesale
true religion sale
coach outlet online
michael kors outlet online
ray ban sunglasses discount
ray ban outlet
toms outlet
louis vuitton outlet
michael kors handbags
jordan retro 3
celine bags
true religion outlet
as
ralph lauren outlet
ReplyDeletemont blanc outlet
jordan 11
coach outlet
uggs outlet
fitflops sale
ugg boots
michael kors outlet
louis vuitton outlet online
michael kors outlet
football shirts
cheap ray ban sunglasses
michael kors outlet
reebok trainers
michael kors handbags clearance
tory burch outlet online
police sunglasses for men
ugg outlet
cheap football shirts
michael kors outlet
polo ralph lauren
cheap nike shoes sale
beats headphones
hermes birkin bag
nike outlet store
kobe bryants shoes
basketball shoes
swarovski crystal
michael kors uk outlet
hermes bags
basketball shoes,basketball sneakers,lebron james shoes,sports shoes,kobe bryant shoes,kobe sneakers,nike basketball shoes,running shoes,mens sport shoes,nike shoes
kate spade uk
michael kors uk
nike trainers
cheap nhl jerseys
20160729caihuali
louis vuitton outlet online
ReplyDeleteugg sale
cheap ugg boots
adidas superstars
louis vuitton
cheap jordan shoes
fitflop shoes
fitflops
christian louboutin outlet
ugg outlet
2016.11.17chenlixiang
louboutin outlet
ReplyDeleteborse louis vuitton
the north face
coach factory outlet
true religion jeans
coach factory outlet
celine outlet
christian louboutin shoes
cheap louis vuitton bags
pandora bracelet
2017.2.20chenlixiang
christian louboutin outlet
ReplyDeletecoach outlet online
pandora jewelry
nike shoes outlet
san francisco giants jerseys
cheap jordans
polo ralph lauren outlet
adidas shoes
louboutin shoes
fred perry
20170313caiyan
gucci
ReplyDeletefitflops sale clearance
coach outlet online
birkenstock uk
dolce and gabbana outlet online
cheap jordan shoes
moncler uk
louboutin shoes
rangers jerseys
adidas outlet store
170522yueqin
Thank you ever so for you article post
ReplyDeleteتابلو استيل
تابلو تبليغاتی
vivavideo pro kinemaster pro picsay pro
ReplyDeletechenqiuying20181012
ReplyDeletecoach factory outlet
canada goose outlet store
ugg outlet
cheap nike shoes
christian louboutin
coach outlet store
ed hardy outlet
michael kors outlet online
chaussure louboutin
michael kors outlet
His article is very helpful at all thanks
ReplyDeleteCara Mengobati bisul
Cara Mengobati Benjolan Pada Mata Dengan Obat Herbal Paling Ampuh
Cara Mengobati Penyakit Gondongan Dengan Bahan Alami
Obat Asam Urat Paling Ampuh
Cara Menghilangkan Keputihan Secara Alami dan Permanen
Cara Mengobati Hematuria
fila shoes
ReplyDeletefake rolex
hermes belt
moncler outlet
longchamp handbags
coach outlet store
nike air force 1 high
lebron shoes
cheap jordans
nmd
ReplyDeleteشركة نقل عفش من الرياض الى دبي
ارخص شركة نقل اثاث بالدمام ارخص شركة نقل اثاث بالدمام
شركة نقل عفش
شركة نقل اثاث من الرياض الى الاردن
_____