I would like to discuss Shell Scripting in Unix in this topic today. What I would be discussing with you is introduction to Shell Scripting, its features, command line parameters, arithmetic operations, control structures and functions usage in Shell Scripting.
Lets start with the definitions. Shell is a program provided by Unix, typically to provide interface to the Unix system. Thus, in other words, Shell is some tool for interacting user with system. Generally its either text based and command line oriented. Command Interpreter which is series of commands and takes commands from user and executes them. There are different types of Shell e..g Bourne Shell (/bin/sh), C Shell (/bin/csh), TC Shell (/bin/tcsh), Korn Shell (/bin/ksh), Bash (Bourne Again shell) (/bin/ksh) etc.
Why do we need Shell Programs ? Nice question you have... Lets concentrate now on this basic question which is generally asked. We generally may need to run tasks that are customized for different system or Some times we need to write some jobs that run on a system and we need to control those jobs we may need Shell Scripting in such and situations like those.
1.Script starts with line #!/bin/sh
2.'#' is used for commenting in script. Comments used in script always makes your code readable
3.It gives identification of author of script and date & time of update.
4.Easy to read and understand
5.Major complex sections of codes can be explained
6.Last but not least Versioning is tracked through this
These are few of features that we can generally think of it.
Lets see now what are the parameters and its usage through command line.
$0 tells you the name of script. $1, $2, $3 … tells you the 1st , 2nd , 3rd and so on parameter passed through the command line. And $# gives you no. of arguments passed to the script. If your script needs to read the input given by user through command prompt, use “read” statement .
e.g. echo “Please enter your name : ”
read name
where name would be the variable holding the user inputted value.
How arithmetic operations are carried out in Scripting is still a question. Let me explain you through the example.
e.g. i=2
j=3
k=`expr $j - $i`
echo “Result of $j - $i is : $k ”
Note here, expr is used to execute the operation and it would assign the result to the variable on left side.
Just like other programming languages, Shell Scripting also provide couple of control structures for your help. Significance of control structures is well knows world wide.. :)
If – then, For loop, While loop, Case statement are couple of control structures provided by Shell Scripting.
Lets see Syntaxes one by one for all Control Structures :
1.If- then
Syntax:
if condition then
condition is satisfied and is true
execute this block
else
if condition is not satisfied then execute this block upto fi.
fi
Here, note that condition is zero means its true and satisfied. And you can go in nested
if -else – fi .
2.For Loop
Syntax:
for { variable name } in { list }
do
execute and repeat all the statements in this block once for each item till
the list is finished
done
3.While Loop
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
done
Here, Loop is executed as long as given condition is true. You need to make sure, at some point condition should get false within the loop or else it would go on executing indefinitely causing script hang.
4.Case Statement
The case statement is good alternative to Multilevel if-then-else-fi statement. It enable you to match several values against one variable. Its easier to read and write.
Syntax:
case $varName in
pattern1) command1
command2
…..
commandn;;
pattern2) command1
command2
…..
commandn;;
.
.
.
.
patternN) command1
command2
…..
commandn;;
*) command;;
esac
The $varName is compared with patterns till a match is found. And similar to case statements in
other programming languages such as PHP, shell executes all the statements up to the two semicolons as a delimiter.. *) is the default case and executed when no pattern is matched.
Hope at this point of line, you have been very much comfortable with Shell Scripting and its basics. Finally lets see how user defined functions work in Shell Scripting.
In Shell Scripting Function can be defined as a series of commands. They performs specific work to do or simply say task. Below mentioned syntax can help you understand the function in detail.
Syntax:
function_name ( )
{
command1
command2
.
.
.
.
commandN
return
}
Here, function_name is the name of function that you define, and it executes series of commands either up to return statement or end curly brace of function (whichever encounters first ) which terminates the function and passes control back to the calling point
e.g.
Type fnGetName() at $ prompt as follows
fnGetName()
{
echo “in function”
}
Call to the above function can be given with simply name of fucntion.
e.g. fnGetName
fuew !! Tired so far reading ?? ha ha .. we are not done yet but for now.. yes..!! This is enough for you to get basic idea of what is Shell Scripting. We still have tons of other areas in Shell Scripting to cover which is vast in size and will need separate site to explain and discuss it. Lets discuss it further later at some point, in mean while you can always get in touch with me for your queries through this blog and will be happy to resolve them.
Till then aloha !!
Friday, March 6, 2009
Thursday, March 5, 2009
Importance of Project Planning in Software Industry
It has been a very controversial discussion on importance of Project Planning in Project Development in Software Industry among my friends. Many of my friends has view that its mere time wasting in documentation rather than actual development. What all I say to them is just a saying.. "The sooner you start, the later you finish !!".. Let me explain what does it mean... If you start project without prior planning and estimation, you will face tons of obstacles and surprising risks and crisis. After huge discussion and debate finally I decided to write an article and would like to take an opportunity to share few of my thoughts about Importance of Project Planning which I realized in my project.
Project Planning process gives a significant help in your project. Even though we know lots of concepts that generally we follow while project development, but if project is not going through proper phases given by CMMi standards, many of activities that we are supposed to follow gets overlooked and later when project finds ample of surprising crisis, we find that following those processes would have really helped for betterment of project.
Project Planning is one of such process. For instance, setting milestones on project, which we as a manager generally overlook in project development. Many of us knows this concept and its usefulness in project, but as mentioned earlier when project starts directly without process following, milestones does not get setup in advance and then it becomes very tedious to trace back the tasks and its completion time versus estimated time. Project Planning process gives this power to you to plan well in advance and in turn easiness to mange the project.
External and Internal dependencies are another factors which we never think about in advance. If we are following Project Planning process and preparing its documents in advance, it will give you chance to think in deep about this area well in advance which generally person do not do when not following this process...
Risk management. This is another key aspect in project development. When project is started directly without prior preparation documents in hand, person literally think on surface level about it. Believe me, and I hope you agree with it, that each and every project has Risk associated to it and Project Planning is the document which gives you change to think about it that are going to occur in future and not on surface level. If project is following the general traditional way of development then manager never foresee those Risks in advance and hence is not well planned for any kind of un-certain disaster with project. And then obviously person heads towards applying patches and it goes on and on and on... This can be easily avoided by planning well in advance, where Project Planning Process helps you a lot...
Third party tool, Roles and Responsibilities and many more such sections from Project Planning, which gives you enough power and makes you think beyond the limit. Hence I suggest you to give proper attention towards Project Planning and filling Project Plan document for all of your projects. I realized its importance at my project hand helped me to complete my project on time.
I hope by the time you are at this point reading, you've already agreed with me and understood how project planning in project development plays vital role. Let me know your views and thoughts on this topic. I would like to discuss further and will be interested in knowing any pros that I've missed to mention or drawback of project planning if you think so.
Project Planning process gives a significant help in your project. Even though we know lots of concepts that generally we follow while project development, but if project is not going through proper phases given by CMMi standards, many of activities that we are supposed to follow gets overlooked and later when project finds ample of surprising crisis, we find that following those processes would have really helped for betterment of project.
Project Planning is one of such process. For instance, setting milestones on project, which we as a manager generally overlook in project development. Many of us knows this concept and its usefulness in project, but as mentioned earlier when project starts directly without process following, milestones does not get setup in advance and then it becomes very tedious to trace back the tasks and its completion time versus estimated time. Project Planning process gives this power to you to plan well in advance and in turn easiness to mange the project.
External and Internal dependencies are another factors which we never think about in advance. If we are following Project Planning process and preparing its documents in advance, it will give you chance to think in deep about this area well in advance which generally person do not do when not following this process...
Risk management. This is another key aspect in project development. When project is started directly without prior preparation documents in hand, person literally think on surface level about it. Believe me, and I hope you agree with it, that each and every project has Risk associated to it and Project Planning is the document which gives you change to think about it that are going to occur in future and not on surface level. If project is following the general traditional way of development then manager never foresee those Risks in advance and hence is not well planned for any kind of un-certain disaster with project. And then obviously person heads towards applying patches and it goes on and on and on... This can be easily avoided by planning well in advance, where Project Planning Process helps you a lot...
Third party tool, Roles and Responsibilities and many more such sections from Project Planning, which gives you enough power and makes you think beyond the limit. Hence I suggest you to give proper attention towards Project Planning and filling Project Plan document for all of your projects. I realized its importance at my project hand helped me to complete my project on time.
I hope by the time you are at this point reading, you've already agreed with me and understood how project planning in project development plays vital role. Let me know your views and thoughts on this topic. I would like to discuss further and will be interested in knowing any pros that I've missed to mention or drawback of project planning if you think so.
Wednesday, March 4, 2009
How to show Google map on your website
Lots of time I have seen that programmer generally wants to show Google maps on their website. I personally think, while going through developing those requirement, maps generally requires longitude and latitude which is hard for a typical developer to provide since requirement generally provides you just address of place of which map to be shown. In that case it becomes challenge for the developer to calculate required
longitude and latitude of a specific address and then accordingly pass them to get the map. Second challenge, developer may face in placing marker on map If developer is new in this area and he has to spend tons of time in R&D for getting these markers placed along with SmartWindow for label on marker. Believe, me this is my personal experience when I as a fresher started working on this requirement have spent lots of time in so simple feature. (Don't call me dumb..please.. ;) )
So hereby, I would give you a small function which will give a facility for a developer to just include couple of functions and map will be ready for him on page with marker and SmartWindow with label details without hassle of calculating long. & lat. details of address.
Very first step obviously you have to do is to get AppId for your application. If you are doing web based development, you need to provide domain URL for authentication. This is extra care taken by Google and appId generated for one domain through Google can not be used for another domain. Well sorry but, it's like that only and I personally love this extra check by Google. Click Here for signing up for a Google Maps API Key. Once you receive your API Key you are ready to go for that !
Next step is to include/load map file from Google using appId generated for specific to your domain.
e.g.
<script type="text/javascript" src="http://www.google.com/jsapi?key=abcdefgh"></script>
where you need to replace your API Key with "abcdefgh" given in example.
and that's it.. Now rest all you have to do is copy below mentioned piece of Javascript code and paste in your file, preferably within <head> </head>tag, below <title></title> tag though it really doesn't matter but just for the sake of coding standards.
= JS Functions to display MAP Starts here =
<!-- Functions for Map -->
<script type="text/javascript">
/* Define the address you want to show map of */
var address = "1600 Amphitheatre Parkway Mountain View, CA 94043";
/* Define the label details of address you want to show as marker on map */
var infoWindowDetails= '<span ><B>Google Inc.</B> <br /> 1600 Amphitheatre Parkway <br /> Mountain View, CA 94043 USA<br />Phone: +1 650-253-0000<br />Fax: +1 650-253-0001 </span>';
/* Load Google Map. second parametere is for view type*/
google.load("maps", "2");
var map = null;
var geocoder = null;
// Call this function when the page has been loaded
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas")); // Creat instance of map.
map.addControl(new GLargeMapControl()); // Add Map Control on map
var mapControl = new GMapTypeControl();
map.addControl(mapControl); // Add Map Type Control on map
geocoder = new GClientGeocoder();
showAddress(); // Call to the fucntion which will mark the address on map.
}
}
function showAddress() {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
var marker = new GMarker(point); // Create instance of marken on map
map.addOverlay(marker); // Add overlay of marker on map
marker.openInfoWindowHtml(infoWindowDetails); // Show label baloon on marken with address details.
}
}
);
}
}
google.setOnLoadCallback(initialize); // Call to callback function on page load.
</script>
= JS Functions to display MAP Ends here =
Congratulations !! you are done with it.. You can easily see your map with specified address... No need to calculate latitude, longitude neither is there hassle of point locations finding for Marker and SmartWindow.
BTW, Dont forget to provide some content layer on your page to hold the map... or else there is no specific place you are giving for google to show its map on your website..
e.g.
<body>
<div id="map_canvas" style="width:600px;height:300px"></div>
</body>
Here's wishing you a very happy programming !!
longitude and latitude of a specific address and then accordingly pass them to get the map. Second challenge, developer may face in placing marker on map If developer is new in this area and he has to spend tons of time in R&D for getting these markers placed along with SmartWindow for label on marker. Believe, me this is my personal experience when I as a fresher started working on this requirement have spent lots of time in so simple feature. (Don't call me dumb..please.. ;) )
So hereby, I would give you a small function which will give a facility for a developer to just include couple of functions and map will be ready for him on page with marker and SmartWindow with label details without hassle of calculating long. & lat. details of address.
Very first step obviously you have to do is to get AppId for your application. If you are doing web based development, you need to provide domain URL for authentication. This is extra care taken by Google and appId generated for one domain through Google can not be used for another domain. Well sorry but, it's like that only and I personally love this extra check by Google. Click Here for signing up for a Google Maps API Key. Once you receive your API Key you are ready to go for that !
Next step is to include/load map file from Google using appId generated for specific to your domain.
e.g.
<script type="text/javascript" src="http://www.google.com/jsapi?key=abcdefgh"></script>
where you need to replace your API Key with "abcdefgh" given in example.
and that's it.. Now rest all you have to do is copy below mentioned piece of Javascript code and paste in your file, preferably within <head> </head>tag, below <title></title> tag though it really doesn't matter but just for the sake of coding standards.
= JS Functions to display MAP Starts here =
<!-- Functions for Map -->
<script type="text/javascript">
/* Define the address you want to show map of */
var address = "1600 Amphitheatre Parkway Mountain View, CA 94043";
/* Define the label details of address you want to show as marker on map */
var infoWindowDetails= '<span ><B>Google Inc.</B> <br /> 1600 Amphitheatre Parkway <br /> Mountain View, CA 94043 USA<br />Phone: +1 650-253-0000<br />Fax: +1 650-253-0001 </span>';
/* Load Google Map. second parametere is for view type*/
google.load("maps", "2");
var map = null;
var geocoder = null;
// Call this function when the page has been loaded
function initialize() {
if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map_canvas")); // Creat instance of map.
map.addControl(new GLargeMapControl()); // Add Map Control on map
var mapControl = new GMapTypeControl();
map.addControl(mapControl); // Add Map Type Control on map
geocoder = new GClientGeocoder();
showAddress(); // Call to the fucntion which will mark the address on map.
}
}
function showAddress() {
if (geocoder) {
geocoder.getLatLng(
address,
function(point) {
if (!point) {
alert(address + " not found");
} else {
map.setCenter(point, 13);
var marker = new GMarker(point); // Create instance of marken on map
map.addOverlay(marker); // Add overlay of marker on map
marker.openInfoWindowHtml(infoWindowDetails); // Show label baloon on marken with address details.
}
}
);
}
}
google.setOnLoadCallback(initialize); // Call to callback function on page load.
</script>
= JS Functions to display MAP Ends here =
Congratulations !! you are done with it.. You can easily see your map with specified address... No need to calculate latitude, longitude neither is there hassle of point locations finding for Marker and SmartWindow.
BTW, Dont forget to provide some content layer on your page to hold the map... or else there is no specific place you are giving for google to show its map on your website..
e.g.
<body>
<div id="map_canvas" style="width:600px;height:300px"></div>
</body>
Here's wishing you a very happy programming !!
Monday, March 2, 2009
Looping through form elements using JavaScript
Here is another basic need of developer. Many times developer needs script to loop through all the elements that are available in form tag using client side script.
Following is the piece of Javascript that can be used on your page under script tag which will help to loop through all the form elements on page. You need to copy and paste the piece of code under <> < /script > tag for Javascript language.
Note, here you need to replace frmName with your actual name of form element on page.
for(i=0; i < document.frmName.elements.length; i++)
{
alert("Field Name : " + document.frmName.elements[i].name + "
<> Field Value : " + document.frmName.elements[i].value + ".
<> ");
}
You can find example of how can above piece can be used to loop through all the form fields. Example explains you how all the checkboxes available in form as well as explains how those checkboxes available on page can be unchecked (e.g. all the records listed on page needs to be activated/de-activated, or adding / removing items from your shopping basket).
Initially, you need actions to give call to the Function...
< type="button" name="btnSelect" value="Select Records" onclick="javascript: selectAll();">
< type="button" name="btnDeSelect" value="De-Select Records" onclick="javascript: deSelectAll();">
Second step is to have the piece of code that loops through all the elements,
< language="JavaScript">
function selectAll()
{
/* Function to loop through the form elements and check all the elements that are of type Checkbox */
for(i=0; i < document.frmName.elements.length; i++)
{
if(document.frmName.elements[i].type=="checkbox")
{
document.frmName.elements[i].checked=true;
}
}
}
function deSelectAll()
{
/* Function to loop through the form elements and un-check all the elements that are of type Checkbox */
for(i=0; i < document.frmName.elements.length; i++)
{
if(document.frmName.elements[i].type=="checkbox")
{
document.frmName.elements[i].checked=false;
}
}
}
< /script >
Following is the piece of Javascript that can be used on your page under script tag which will help to loop through all the form elements on page. You need to copy and paste the piece of code under <> < /script > tag for Javascript language.
Note, here you need to replace frmName with your actual name of form element on page.
for(i=0; i < document.frmName.elements.length; i++)
{
alert("Field Name : " + document.frmName.elements[i].name + "
<> Field Value : " + document.frmName.elements[i].value + ".
<> ");
}
You can find example of how can above piece can be used to loop through all the form fields. Example explains you how all the checkboxes available in form as well as explains how those checkboxes available on page can be unchecked (e.g. all the records listed on page needs to be activated/de-activated, or adding / removing items from your shopping basket).
Initially, you need actions to give call to the Function...
< type="button" name="btnSelect" value="Select Records" onclick="javascript: selectAll();">
< type="button" name="btnDeSelect" value="De-Select Records" onclick="javascript: deSelectAll();">
Second step is to have the piece of code that loops through all the elements,
< language="JavaScript">
function selectAll()
{
/* Function to loop through the form elements and check all the elements that are of type Checkbox */
for(i=0; i < document.frmName.elements.length; i++)
{
if(document.frmName.elements[i].type=="checkbox")
{
document.frmName.elements[i].checked=true;
}
}
}
function deSelectAll()
{
/* Function to loop through the form elements and un-check all the elements that are of type Checkbox */
for(i=0; i < document.frmName.elements.length; i++)
{
if(document.frmName.elements[i].type=="checkbox")
{
document.frmName.elements[i].checked=false;
}
}
}
< /script >
Monday, January 26, 2009
Eliminate HTML Tags submitted by User
It's general error overlooked by developers, that while coding a general text box is given for entering the user details to the form, but its not taken care that, what if user enters the HTML tags into the text box.
If developer is using the database to store those details, then MySql and PHP settings might take care of adding the data to database correctly, but while rendering the data onto a web page, it gets
overlooked. Server simply declare them as mere HTML character and send it behind the code.
Which in turn causes the application crash..
Additionally, in case of making site SQL Injection proof, developer really need to take care of HTML post on site and eliminate or replace those HTML Characters.
Below is simple script I have written to either remove or replace the HTML characters from posted data. This script is simple and with minimal size of code for achieving the target.
// Start of script
Usage : validateData([param1]);
==============================
function replaceHTML(&$value,$key)
{
$value=htmlentities($value);
}
function removeHTML(&$value,$key)
{
$value=strip_tags($value);
}
function validateData($replaceOption='replace')
{
$arrPostedData=array_merge($_POST,$_GET);
switch($replaceOption)
{
case "replace" : array_walk($arrPostedData,'replaceHTML');
break;
case "remove" : array_walk($arrPostedData,'removeHTML');
break;
}
echo "<>";
print_r($arrPostedData);
echo "< /pre >";
}
e.g.
validateData('replace');
validateData('remove');
validateData();
=====================
Above piece of code in PHP which will help developer to have parse the inputted data from user and will either give option to completely remove the HTML tags off the data or will give feasibility to replace those tags with its equivalent...
Parameter sent while calling function will decide what to do...This parameter is optional and would take bydefault replace if nothing passed while calling... moreover, developer can customize the function as per his/her need...
If developer is using the database to store those details, then MySql and PHP settings might take care of adding the data to database correctly, but while rendering the data onto a web page, it gets
overlooked. Server simply declare them as mere HTML character and send it behind the code.
Which in turn causes the application crash..
Additionally, in case of making site SQL Injection proof, developer really need to take care of HTML post on site and eliminate or replace those HTML Characters.
Below is simple script I have written to either remove or replace the HTML characters from posted data. This script is simple and with minimal size of code for achieving the target.
// Start of script
Usage : validateData([param1]);
==============================
function replaceHTML(&$value,$key)
{
$value=htmlentities($value);
}
function removeHTML(&$value,$key)
{
$value=strip_tags($value);
}
function validateData($replaceOption='replace')
{
$arrPostedData=array_merge($_POST,$_GET);
switch($replaceOption)
{
case "replace" : array_walk($arrPostedData,'replaceHTML');
break;
case "remove" : array_walk($arrPostedData,'removeHTML');
break;
}
echo "<>";
print_r($arrPostedData);
echo "< /pre >";
}
e.g.
validateData('replace');
validateData('remove');
validateData();
=====================
Above piece of code in PHP which will help developer to have parse the inputted data from user and will either give option to completely remove the HTML tags off the data or will give feasibility to replace those tags with its equivalent...
Parameter sent while calling function will decide what to do...This parameter is optional and would take bydefault replace if nothing passed while calling... moreover, developer can customize the function as per his/her need...
Sunday, January 25, 2009
Sending class object into email body using ob_* functions
It has been several times seen in my past career that developers ( mostly newbie) generally find much difficulty in sending the content of class object through email... Occasionally, it has been seen that even though we can print the object content on browser and can view the object content and the values the object is holding, it becomes sometime necessity that we get the content of object through email....
Following is the piece of code, which will definitely be helpful for your development...
// Start of code
ob_start();
$strBody="Hi, There is an Error in creating Class Object while script was in second loop. Record Id :".$records["recordDetailsId"];
print_r($objClass);
$strBody.=ob_get_contents();
ob_end_flush();
mail("foo@mycompany.com","Error in Creating Object ",$strBody,"From: My Site\n");
// End of code
So you can easily find how only three functions i.e. ob_start() , ob_get_contents() and ob_end_flush() serve the purpose ...
More details about these functions can be found at php.net
Following is the piece of code, which will definitely be helpful for your development...
// Start of code
ob_start();
$strBody="Hi, There is an Error in creating Class Object while script was in second loop. Record Id :".$records["recordDetailsId"];
print_r($objClass);
$strBody.=ob_get_contents();
ob_end_flush();
mail("foo@mycompany.com","Error in Creating Object ",$strBody,"From: My Site
// End of code
So you can easily find how only three functions i.e. ob_start() , ob_get_contents() and ob_end_flush() serve the purpose ...
More details about these functions can be found at php.net
Subscribe to:
Posts (Atom)