Saturday 14 May 2011

Bash Scripting | Beginner Tutorial


In this beginner tutorial I will show you some of the basics of shell script programming, and hopefully to introduce some of the possibilities of simple but powerful programming available under the shell.
Bash scripting is very useful, with simple commands you can do a lot, for example: create folder with name of current date , count lines, find words in files, search files and many more.
1)MKDIR
- mkdir dirname is bash command for creating folders
-dirname is the name of the directory that you wish to create
Example 1:
$ mkdir /home/ivan/hackspc.com/folder1
$ mkdir /home/ivan/hackspc.com/folder2
$ mkdir /home/ivan/hackspc.com/folder3

Example 2: Create folder with name of current date
mkdir $(date “+%d.%m.%Y”)

Example 3: Create folder with name of current day
$ mkdir $(date “+%A”)

Example 4: Create folders using brace expansion
$ mkdir /home/ivan/hackspc.com/{1..10}

$ mkdir /home/ivan/hackspc.com/project{1..10}

$ mkdir /home/ivan/hackspc.com/web{tools,masters,deseign}

Example 5: make all parent directories along with their children in a single command.
$ mkdir -p programming/BASHscripting/tutorials/StepByStep

$ mkdir -p programming/BASHscripting/tutorials/StepByStep is same as
~ $ mkdir programming
~ $ cd programming
~/programming $ mkdir BASHscripting
~/programming $ cd BASHscripting
~/programming/BASHscripting $ mkdir tutorials
~/programming/BASHscripting $ cd tutorials
~/programming/BASHscripting/tutorials/ $ mkdir StepByStep
~/programming/BASHscripting/tutorials/ $ cd StepByStep
As you can see above, mkdir with -p option can save you a lot of time an efforts.
2)WC (word count)
- wc displays a count of lines, words, and characters in a file.
-wc [options] [file]
-options: -c Count charachters
-l Count lines
-w Count words
- without options count Lines Words Characters
-in examples we’ll use file hackspc.txt as show below

Example 1: Count lines in hackspc.txt
$ wc -l hackspc.txt
Output:

Example 2: Count words in hackspc.txt
$ wc -w hackspc.txt
Output:

Example 3: Count characters in hackspc.txt
$ wc -c hackspc.txt
Output:

Example 4: Count lines, words, characters
$ wc hackspc.txt

Example 5: count number of lines, words, characters from hackspc.txt and save it into filecounting.txt
$ wc hackspc.txt>filecounting.txt
Output: filecounting.txt

3)GREP
-grep command is one of the easiest methods of locating text contained within a file
-grep [options] PATTERN [FILE...]
Options:
-v invert the sense of matching, to select non-matching lines
-i ignore case distinctions in both the PATTERN and the input files
-c Suppress normal output; instead print a count of matching lines for each input file
-E Interpret PATTERN as an extended regular expression
-q do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
Let’s look .txt file called hackspc.txt

Example 1 : find word hack in file hackspc.txt,
$ grep hack hackspc.txt
Output will be all lines that contains word hack

This command is case sensitive, so hack isn’t same as Hack
Example 2: find lines that don’t contain word hack
$ grep hack hackspc.txt
Output:

Example 3 : find word hack in file hackspc.txt, command isn’t case sensitive
$ grep -i hack hackspc.txt
Output will be all lines that contains word hack, Hack, HACK,hAcK,HaCK…

Example 4 : find lines that contains words that end with g
$ grep -E “[a-z]+g” hackspc.txt
Output:

In this example I used regular expression , [a-z] all combinations of charachters a,b,c,d,e…z ,
+ repeat at least one or more. Regular expressions are very useful and complicated. In this beginner tutorial I will not write about them.
4)FIND
- Linux / Unix and their variants have several different ways of locating files find, locate, wheris. I mostly use find command. It offer a lot options and it can easly search for files that meet a desired criteria.
- find [option] [directory] [conditions]
-conditions:
-name true if pattern matches the current file name
-type -d (find folders) , -f (find files)
-size
-mtime n True if the file’s data was modified n days ago.
-regex regular expression
Example 1: Search for any file named hackspc.txt in the directory home/ivan/hackspc.com/ and any subdirectory.

$ find /home/ivan/hackspc.com -name hackspc.txt
Output:

Example 2: search for any file that is larger then 2k

$ find /home/ivan/hackspc.com -name ‘*’ -size +2k
Output:

Example 3: search folders

$ find /home/ivan/hackspc.com -type d
Output:

Example 4: Search .txt files that contains only 4 digits (dddd)

$ find /home/ivan/hackspc.com -regex “.*/[0-9][0-9][0-9][0-9]\.txt”
Output:

5)IF, ECHO, VARIABLES
Example 1: if variable a equal to 5 then print five if not then write not five
$ if [ "$a" = 5 ]; then echo “five”; else echo “not five”; fi
Output :

Output :

Example 2: If you find file called hackspc.txt in diretcory /home/ivan/hackspc.com/ then print “I found file called hackspc.txt” if not then print “File Not Found”
$ if find /home/ivan/hackspc.com -name hackspc.txt
lled hackspc.txt”; else echo “File Not Found”; fi

Posted: 15 Apr 2010 04:31 AM PDT
Last time I taught you, how to create your own homepage in HTML. Today I’ll teach you how to create more dynamic home page using Personal Home Page (PHP) scripting.
Please refer to my previous article about creating your own homepage in HTML to get more clearer to web programming, you’ll be needing HTML even though you program it in PHP.
Previously in the tutorial, how to create your own homepage in HTML, we created whole the navigation bar, menu content, header and post contents in one single static page.
This time we are going dynamic, i.e. every part of your web page, header, footer, body, menu, etc are going to change dynamically. To do so we are going to load separate html file in a single html page.
Refer to the chart below for more clarification.


Index.php is the main page which must be loaded by the visitor. Header.html, menu.html, post.html are the Head banner, Menu bar and Posts/articles respectively in separate HTML file. Whilist style.css is the CSS style for all the pages that will be loading in index.php.
Simply we’ll load all the files in index.php.
Let’s start designing the layout for index.php using HTML.
<html>
<head>
<title> Page 1 </title>
</head>
<body>
Header for the web page
<!– Table for Main Body –>
<table border=”0″ width=”100%” cellspacing=”0″ cellpadding=”2″>
<tr>
<td valign=”top” align=”left” width=”90″>
Menu bar at top for page
</td>
<td width=”1″ bgcolor=”lightskyblue” valign=”top”>  </td>
<td valign=”top”>
Main content or article for your home page.
<br> <br>
Same menubar at bottom
</td>
<td width=”1″ bgcolor=”lightskyblue” valign=”top”>   </td>
</tr></table>
</body></html>
I used simple technique of tables to layout the index.php.
Now the layout is ready and we got the maps to assign the specific header, post, menus etc.
Let’s assign the specific file to the specific address.
<html>
<head>
<title> Home Page </title>
<link rel=”stylesheet” href=”style.css”/>
</head>
<body>
<?php
include (“header.html”);
?>

<!– Table for Main Body –>
<table border=”0″ width=”100%” cellspacing=”0″ cellpadding=”2″>
<tr>
<td valign=”top” align=”left” width=”90″>
<?php
include (“menu.html”);
?>

</td>
<td width=”1″ bgcolor=”lightskyblue” valign=”top”>  </td>
<td valign=”top”>
<?php
include (“post.html”);
?>

<br> <br>
<?php
include (“menu.html”);
?>

</td>
<td width=”1″ bgcolor=”lightskyblue” valign=”top”>   </td>
</tr></table>
</body></html>
You can see two differences from the HTML code before and after.
First difference is, <link rel=”stylesheet” type=”text/css” href=”style.css”/> which must lie inside the <head> tag.

What it actually does is, loading the CSS style to index.php.
It’s syntax is:
<link rel=”stylesheet” type=”text/css” href=”#”/>
‘#’ refers to the location of your stylesheet document.
Another is four blocks of code:
<?php
include (“XXX”);
?>

The above code loads the specific html/php or web file wherever it is placed, it is actually a php code that’s why it is enclosed in <?php ….?> or <?….. ?>
We’ve used include (); function to call the specific file, where XXX in the example above refers to the location of the file.
Note: If all the files including sub-files contains in same directory, you don’t need to mention the URL of the file, else you must define the exact location for it.

We’ve basic layout for homepage ready. All we need is the sub-pages.
Design a good banner for your homepage using Photoshop or other Image editing software.
Create new text document with following inputs in it:
<img src=”image/banner.jpg” border=”0”/>
Save it as header.html.
Create another text file with following inputs:
<a href=”index.php”>Home </a> |
<a href=”page2.php”>Page</a> |
<a href=”page3.php”>About us</a> |
<a href=”page4.php”>Contact</a>
Save it as menu.html.
Now create a another text document where your contents will be loaded,
For example,
<h2> H2 heading for the page </h2>
Here goes the main content of your web page. You can include any information about your page here.
Example for a horizontal line = <hr>
Example using ‘class’ = ‘note’:
<span> Example using ‘class’ = ‘note’ </span>
Now let’s put in an image (that is in the same folder as this file so we don’t have to put the full path / URL
<img src=”images/anyimage.jpg” border=”0″ width=”100″ height=”65″ alt=”alternate_text”>
Save the above example as post.html
Now you have all the contents ready. Anything left is the styling part.
You can also create your own style in another text document and save it as filename.css (in this tutorial we’ll call it style.css).
Copy the code and paste it in stye.css that you’ve just created.
body {
background-color: #FFFFFF;
font-family: Arial, Verdana, sans-serif;
font-size: 18px;
}
a { font-family: Arial, Verdana, sans-serif; font-size: 18px; color: #483D8B; text-decoration: underline}
a:hover { font-family: Arial, Verdana, sans-serif; font-size: 18px; color: #000000; background-color: #FAEBD7}
h1 { font-family: Arial, Verdana, sans-serif; font-size: 36px; color: #000000 }
h2 { font-family: Arial, Verdana, sans-serif; font-size: 24px; color: #000000 }
h3 { font-family: Arial, Verdana, sans-serif; font-size: 24px; color: #000000 }
h4 { font-family: Arial, Verdana, sans-serif; font-size: 18px; color: #000000 }
h5 { font-family: Arial, Verdana, sans-serif; font-size: 12px; color: #000000 }
h6 { font-family: Arial, Verdana, sans-serif; font-size: 8px; color: #000000 }
hr{ color:brown; background-color:tan; width:90%; height:2px; }
table { font-family: Arial, Verdana, sans-serif; font-size: 18px; color: #00008B; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px}
.note { font-family: Arial, Verdana, sans-serif; font-size: 14px; color: purple;
font-weight: bold;}
Save it in style.css.
I’ve applied the styling for only body, horizontal rule(hr), table and custom class named note.
Your final page will have the format similar to below:

You can add and replace the menu.html and other without opening index.php this is what makes your page handling experience some what dynamic.
Hope you liked this tutorial.

No comments:

Post a Comment