TechView with Kamlesh khaliya

Resources for OCP, Oracle Interviews(kamleshkhaliya@gmail.com)

Thursday, 30 August 2012

Interview Questions and Answers Java Script


Q. What’s relationship between JavaScript and ECMA Script?
Ans – ECMA Script is yet another name for JavaScript (other names include Live Script). The current JavaScript that you see supported in browsers is ECMA Script version 3.
Q. What are JavaScript types?
Ans - Number, String, Boolean, Function, Object, Null, Undefined.

Q. How do you convert numbers between different bases in JavaScript?
Ans - Use the parseInt() function, that takes a string as the first parameter, and the base as a second parameter. So to convert hexadecimal 3F to decimal, use parseInt ("3F", 16);

Q. What does isNaN function do?
Ans - Return true if the argument is not a number.
Q. What is negative infinity?
Ans - It’s a number in JavaScript, derived by dividing negative number by zero.

Q. What boolean operators does JavaScript support?
Ans - &&, || and !
Q. What does "1"+2+4 evaluate to?
Ans - Since 1 is a string, everything is a string, so the result is 124.
Q. How about 2+5+"8"?
Ans - Since 2 and 5 are integers, this is number arithmetic, since 8 is a string, it’s concatenation, so 78 is the result.
Q. What about '7' - 4 ?
Ans – This will give 3.

Q. What looping structures are there in JavaScript?
Ans - for, while, do-while loops, but no foreach.

Q. How do you create a new object in JavaScript?
Ans - var obj = new Object(); or var obj = {};
Q. How do you assign object properties?
Ans - obj["age"] = 17 or obj.age = 17.
Q. What’s a way to append a value to an array?
Ans - arr[arr.length] = value;
Q. What is this keyword?
Ans - It refers to the current object.

Q. What is the delete operator in javascript?
Ans - The delete operator is used to delete an object, an object's property or a specified element in an array, returning true if the operation is possible, and false if not. With the defined object 'fruit' below, the following delete operations are possible:
Code:
fruit = new Object;
fruit.name = 'apple';
fruit.color = 'green';
fruit.size = 'large';
delete fruit.size;
with(fruit)
delete color;
delete fruit;

The delete operator can also be used to delete an element of an array. This does not affect the length of the array or any of the other elements but changes the deleted element to undefined.
Code:
fruit = new Array ("apple", "pear", "orange", "cherry", "grape");
delete fruit[2];

Q. What does javascript null mean?
The null value is representing no value or no object.
It implies no object,or null string,no valid boolean value,no number and no array object.

Q.What is the difference between null and undefined in javascript?
Ans - In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:
var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined
Where as null is an assignment value. It can be assigned to a variable as a representation of no value:
var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object
undefined and null are two distinct things: undefined is a type itself (undefined) while null is an object.

Q. What is triple equility (= = = ) in java script?
Ans - Triple equility means equality checking with out the type conversion, also known as strict equality check.
For strict equality check the objects being compared must have the same type also.
For example:
0 = = false // true
0 = = = false // false, because they are of a different type
1 = = "1" // true, auto type coersion
1 = = = "1" // false, because they are of a different type

Q. How to create arrays in JavaScript?
var scripts = new Array();
scripts[0] = "PHP";
scripts[1] = "ASP";
scripts[2] = "JavaScript";
scripts[3] = "HTML";
Now our array scrips has 4 elements inside it and we can print or access them by using their index number. Note that index number starts from 0. To get the third element of the array we have to use the index number 2 . Here is the way to get the third element of an array.
document.write(scripts[2]);
We also can create an array like this
var no_array = new Array(21, 22, 23, 24, 25);

Q. How do you target a specific frame from a hyperlink?
Include the name of the frame in the target attribute of the hyperlink. <a href=”mypage.htm” target=”myframe”>>My Page</a>
Q. Where are cookies actually stored on the hard disk?
This depends on the user's browser and OS.
In the case of Netscape with Windows OS,all the cookies are stored in a single file called

cookies.txt
c:\Program Files\Netscape\Users\username\cookies.txt
In the case of IE,each cookie is stored in a separate file namely username@website.txt.
c:\Windows\Cookies\username@Website.txt 

Q. How to get the contents of an input box using Javascript?
Use the "value" property.
var myValue = window.document.getElementById("MyTextBox").value;

Q. How to determine the state of a checkbox using Javascript?
var checkedP = window.document.getElementById("myCheckBox").checked;

Q. How to set the focus in an element using Javascript?
<script> function setFocus() { if(focusElement != null) { document.forms[0].elements["myelementname"].focus(); } } </script>

Q. How to access an external javascript file that is stored externally and not embedded?
This can be achieved by using the following tag between head tags or between body tags.
<script language='javascript' src='js/sqltohtml.js'></script>

Q. What is the difference between an alert box and a confirmation box?
An alert box displays only one button which is the OK button whereas the Confirm box displays two buttons namely OK and cancel. So from confirmation box we can get a response from user.

Q. What is a prompt box?
A prompt box allows the user to enter input by providing a text box.

Q. Can javascript code be broken in different lines?
Breaking is possible within a string statement by using a backslash \ at the end but not within any other javascript statement.
that is ,
document.write("Hello \ world");
is possible but not document.write \
("hello world");

Q. What is the difference between SessionState and ViewState?
ViewState is specific to a page in a session. Session state refers to user specific data that can be accessed across all pages in the web application.

Q. To put a "close window" link on a page ?
<a href='javascript:window.close()' class='mainnav'> Close </a>

Q. How to comment javascript code?
Use // for line comments and
/*

*/ for block comments

Q. Name the numeric constants representing max,min values
Number.MAX_VALUE
Number.MIN_VALUE

Q. To set all checkboxes to true using JavaScript?
//select all input tags
function SelectAll()
{
var checkboxes = document.getElementsByTagName("input");
for(i=0;i<checkboxes.length;i++)
{
if(checkboxes.item(i).attributes["type"].value == "checkbox")
{
checkboxes.item(i).checked = true;
}
}
}

Q. What is variable typing in javascript?
It is perfectly legal to assign a number to a variable and then assign a string to the same variable as follows
example
i = 10;
i = "string";
This is called variable typing

Q. How to disable an HTML object ?
document.getElementById("myObject").disabled = true;

Q. To write messages to the screen without using "document.write()" ?
Changing the contents of an element is a much better solution. When the method showStatus is invoked it will change the content of the span.
...
function showStatus(message) {
var element = document.getElementById("mystatus");
element.textContent = message; //for Firefox
element.innerHTML = message; //for IE (why can't we all just get along?)
return true;
}
...
<span id="mystatus">Test. </span>
...

Q. How to Add new elements dynamically ?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>t1</title>
<script type="text/javascript">
function addNode() {
var newP = document.createElement("p");
var textNode = document.createTextNode(" I'm a new text node");
newP.appendChild(textNode);
document.getElementById("firstP").appendChild(newP);
}
</script>
</head>

<body onload="addNode();" style=" background: url('../images/Sand-1280.jpg'); background-color: yellow;">

<p id="firstP">firstP<p>

</body>
</html>

Q. How to create a popup warning boxalert('Warning: Please enter an integer between 0 and 100.');

Q. How to create a confirmation box?
confirm("Do you really want to launch the missile?"); 

Q. How to create an input box?
prompt("What is your temperature?");

Q. How to make elements invisible ?
Change the "visibility" attribute of the style object associated with your element. Remember that a hidden element still takes up space, use "display" to make the space disappear as well.
if ( x == y) {
myElement.style.visibility = 'visible';
} else {
myElement.style.visibility = 'hidden';
}

Q. How to set the cursor to wait ?
In theory, we should cache the current state of the cursor and then put it back to its original state.
document.body.style.cursor = 'wait';
//do something interesting and time consuming
document.body.style.cursor = 'auto';

Q. How to reload the current page ?
window.location.reload(true);

Q. how to force a page to go to another page using JavaScript ?
<script language="JavaScript" type="text/javascript" ><!-- location.href="http://newhost/newpath/newfile.html"; //--></script>

Q. How to convert a string to a number using JavaScript?You can use the parseInt() and parseFloat() methods. Notice that extra letters following a valid number are ignored, which is kinda wierd but convenient at times.
parseInt("100") ==> 100
parseFloat("98.6") ==> 98.6
parseFloat("98.6 is a common temperature.") ==> 98.6
parseInt("aa") ==> Nan //Not a Number
parseInt("aa",16) ==> 170 //you can supply a radix or base

Q. How to convert numbers to strings using JavaScript?
You can prepend the number with an empty string
var mystring = ""+myinteger;
or
var mystring = myinteger.toString();
You can specify a base for the conversion,
var myinteger = 14;
var mystring = myinteger.toString(16);
mystring will be "e".

Q. What's Math Constants and Functions using JavaScript?
The Math object contains useful constants such as Math.PI, Math.E
Math.abs(value); //absolute value
Math.max(value1, value2); //find the largest
Math.random() //generate a decimal number between 0 and 1
Math.floor(Math.random()*101) //generate a decimal number between 0 and 100

Q. What's the Date object using JavaScript?
Time inside a date object is stored as milliseconds since Jan 1, 1970.
new Date(06,01,02) // produces "Fri Feb 02 1906 00:00:00 GMT-0600 (Central Standard Time)"
new Date(06,01,02).toLocaleString() // produces "Friday, February 02, 1906 00:00:00"
new Date(06,01,02) - new Date(06,01,01) // produces "86400000"

Q. How to use "join()" to create a string from an array using JavaScript?
"join" concatenates the array elements with a specified seperator between them.
<script type="text/javascript">
var days = ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"];
document.write("days:"+days.join(","));
</script>
This produces
days:Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday

Q. eval()?
The eval() method is incredibly powerful allowing you to execute snippets of code during execution.
<script type="text/javascript">
var USA_Texas_Austin = "521,289";
document.write("Population is "+eval("USA_"+"Texas_"+"Austin"));
</script>
This produces
Population is 521,289

Q. How to create a function using function constructor?
It creates a function called square with argument x and returns x multiplied by itself.
var square = new Function ("x","return x*x");

Q. unescape(), escape()
These are similar to the decodeURI() and encodeURI(), but escape() is used for only portions of a URI.
<script type="text/javascript">
var myvalue = "Sir Walter Scott";
document.write("Original myvalue: "+myvalue);
document.write("<br />escaped: "+escape(myvalue));
document.write("<br />uri part: \"&author="+escape(myvalue)+"\"");
</script>

If you use escape() for the whole URI... well bad things happen.
<script type="text/javascript">
var uri = "http://www.google.com/search?q=sonofusion Taleyarkhan"
document.write("Original uri: "+uri);
document.write("
escaped: "+escape(uri));
v/script>

Q. decodeURI(), encodeURI() Many characters cannot be sent in a URL, but must be converted to their hex encoding. These functions are used to convert an entire URI (a superset of URL) to and from a format that can be sent via a URI.
<script type="text/javascript">
var uri = "http://www.google.com/search?q=sonofusion Taleyarkhan"
document.write("Original uri: "+uri);
document.write("<br />encoded: "+encodeURI(uri));
</script>

Are Java and JavaScript the same?
NO!

Java and JavaScript are two completely different languages in both concept and design!
Java (developed by Sun Microsystems) is a powerful and much more complex programming language - in the same category as C and C++.


What are the benifits of Javascript as a Client side script?
•JavaScript can manipulate HTML elements - A JavaScript can read and change the content of an HTML element
•JavaScript can be used to validate data - A JavaScript can be used to validate form input
•JavaScript can be used to detect the visitor's browser - A JavaScript can be used to detect the visitor's browser, and - depending on the browser - load another page specifically designed for that browser
•JavaScript can be used to create cookies - A JavaScript can be used to store and retrieve information on the visitor's computer

The HTML <script> tag is used to insert a JavaScript into an HTML document.
The HTML "id" attribute is used to identify HTML elements.

You can place an unlimited number of scripts in your document, and you can have scripts in both the body and the head section at the same time.
It is a common practice to put all functions in the head section, or at the bottom of the page. This way they are all in one place and do not interfere with page content.

Using an External JavaScript - JavaScript can also be placed in external files.
External JavaScript files often contain code to be used on several different web pages.
External JavaScript files have the file extension .js.
Note: External script cannot contain the <script></script> tags!

To use an external script, point to the .js file in the "src" attribute of the <script> tag:

<!DOCTYPE html>
<html>
<body>
<script type="text/javascript" src="myScript.js"></script>
</body>
</html>


JavaScript Statements - A JavaScript statement is a command to a browser. The purpose of the command is to tell the browser what to do.

This JavaScript statement tells the browser to write "Hello Dolly" to an HTML element:
document.getElementById("demo").innerHTML="Hello Dolly";

It is normal to add a semicolon at the end of each executable statement. The semicolon is optional (according to the JavaScript standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this you will often see examples without the semicolon at the end.

Note: Using semicolons makes it possible to write multiple statements on one line.


JavaScript Variables - Variables are "containers" for storing information.
Rules for JavaScript variable names:
•Variable names are case sensitive (y and Y are two different variables)
•Variable names must begin with a letter, the $ character, or the underscore character

You declare JavaScript variables with the var keyword:
var carname;

To assign a value to the variable, use the equal (=) sign:
carname="Volvo";

However, you can also assign a value to the variable when you declare it:
var carname="Volvo";

Local JavaScript Variables - A variable declared within a JavaScript function becomes LOCAL and can only be accessed within that function. (the variable has local scope).
Local variables are deleted as soon as the function is completed.

Global JavaScript Variables - Variables declared outside a function, become GLOBAL, and all scripts and functions on the web page can access it.

Global variables are deleted when you close the page.

If you assign values to variables that have not yet been declared, the variables will automatically be declared as global variables.

This statement:
carname="Volvo";
will declare the variable carname as a global variable (if it does not already exist).

Equality Check in JavaScript – We have two operators (= =, = = = ) for equality check.

= = is used only for equality checking of of values for two operands.
Var x = 5
X= =8 will evaluate to false.
X= =5 will evaluate to true

= = = is used for checking the equality of values for both operant as well the type of the operand, so both must be from same type (value and type)
X= = ="5" will evaluate to false, because x is having number where as we are comparing with string.
X= = =5 will evaluate to true


JavaScript If...Else Statements - In JavaScript we have the following conditional statements:

•if statement - use this statement to execute some code only if a specified condition is true
•if...else statement - use this statement to execute some code if the condition is true and another code if the condition is false
•if...else if....else statement - use this statement to select one of many blocks of code to be executed
•switch statement - use this statement to select one of many blocks of code to be executed
if (condition)
{
code to be executed if condition is true
}

switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}


JavaScript Popup Boxes - JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.
Alert Box - An alert box is often used if you want to make sure information comes through to the user.
When an alert box pops up, the user will have to click "OK" to proceed.

Syntax is :
alert("sometext");

Confirm Box - A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns false.

Syntax is :
confirm("sometext");
Example
var r=confirm("Press a button");
if (r==true)
{
x="You pressed OK!";
}
else
{
x="You pressed Cancel!";
}

Prompt Box - A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box returns null.

Syntax is :
prompt("sometext","defaultvalue");

Example
var name=prompt("Please enter your name","Harry Potter");
if (name!=null && name!="")
{
x="Hello " + name + "! How are you today?";
}

To display line breaks inside a popup box, use a back-slash followed by the character n.
Example
alert("Hello\nHow are you?");

JavaScript Functions - A function is a block of code that executes only when you tell it to execute.
It can be when an event occurs, like when a user clicks a button, or from a call within your script, or from a call within another function.

Functions can be placed both in the <head> and in the <body> section of a document, just make sure that the function exists, when the call is made.

Function with Arguments
function myFunction(var1,var2)
{
some code
}

Functions With a Return Value
function myFunction()
{
var x=5;
return x;
}

The for Loop - The for loop is used when you know in advance how many times the script should run.

Syntax
for (variable=startvalue;variable<endvalue;variable=variable+increment)
{
code to be executed
}

Note: The increment parameter could also be negative, and the < could be any comparing statement.

Example
for (i=0; i<5; i++)
{
x=x + "The number is " + i + "<br />";
}


The while Loop - The while loop loops through a block of code while a specified condition is true.

Syntax
while (variable<endvalue)
{
code to be executed
}Note: The < could be any comparing operator.

Example
while (i<5)
{
x=x + "The number is " + i + "<br />";
i++;
}


The do...while Loop - The do...while loop is a variant of the while loop. This loop will execute the block of code once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.

Syntax
do
{
code to be executed
}
while (variable<endvalue);Example
The example below uses a do...while loop. The do...while loop will always be executed at least once, even if the condition is false, because the statements are executed before the condition is tested:

Example
do
{
x=x + "The number is " + i + "<br />";
i++;
}
while (i<5);


The break Statement
The break statement will break the loop and continue executing the code that follows after the loop (if any).

Example
for (i=0;i<10;i++)
{
if (i==3)
{
break;
}
x=x + "The number is " + i + "<br />";
}


The continue Statement
The continue statement will break the current iteration and continue the loop with the next value.

Example
for (i=0;i<=10;i++)
{
if (i==3)
{
continue;
}
x=x + "The number is " + i + "<br />";
}


JavaScript For...In Statement - The for...in statement loops through the properties of an object.

Syntax
for (variable in object)
{
code to be executed
}Note: The block of code inside of the for...in loop will be executed once for each property.

Example
Looping through the properties of an object:

Example
var person={fname:"John",lname:"Doe",age:25};

for (x in person)
{
txt=txt + person[x];
}


Events - By using JavaScript, we have the ability to create dynamic web pages. Events are actions that can be detected by JavaScript.

Every element on a web page has certain events which can trigger a JavaScript. For example, we can use the onClick event of a button element to indicate that a function will run when a user clicks on the button. We define the events in the HTML tags.

Examples of events:

•A mouse click
•A web page or an image loading
•Mousing over a hot spot on the web page
•Selecting an input field in an HTML form
•Submitting an HTML form
•A keystroke
Note: Events are normally used in combination with functions, and the function will not be executed before the event occurs!

onLoad and onUnload -The onLoad and onUnload events are triggered when the user enters or leaves the page.

The onLoad event is often used to check the visitor's browser type and browser version, and load the proper version of the web page based on the information.

Both the onLoad and onUnload events are also often used to deal with cookies that should be set when a user enters or leaves a page. For example, you could have a popup asking for the user's name upon his first arrival to your page. The name is then stored in a cookie. Next time the visitor arrives at your page, you could have another popup saying something like: "Welcome John Doe!".

onFocus, onBlur and onChange - The onFocus, onBlur and onChange events are often used in combination with validation of form fields.

Below is an example of how to use the onChange event. The checkEmail() function will be called whenever the user changes the content of the field:

<input type="text" size="30" id="email" onchange="checkEmail()" />

OnSubmit - The onSubmit event is used to validate ALL form fields before submitting it.

Below is an example of how to use the onSubmit event. The checkForm() function will be called when the user clicks the submit button in the form. If the field values are not accepted, the submit should be cancelled. The function checkForm() returns either true or false. If it returns true the form will be submitted, otherwise the submit will be cancelled:

<form method="post" action="xxx.htm" onsubmit="return checkForm()">

OnMouseOver - The onmouseover event can be used to trigger a function when the user mouses over an HTML element:


JavaScript Try...Catch Statement

The try...catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs.

Syntax
try
{
//Run some code here
}
catch(err)
{
//Handle errors here
}

function message()
{
try
{
adddlert("Welcome guest!");
}
catch(err)
{
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert(txt);
}
}


The Throw Statement
The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.

Syntax
throw exception

The exception can be a string, integer, Boolean or an object.

<script type="text/javascript">
var x=prompt("Enter a number between 5 and 10:","");
try
{
if(x>10)
{
throw "Err1";
}
else if(x<5)
{
throw "Err2";
}
else if(isNaN(x))
{
throw "Err3";
}
}
catch(err)
{
if(err=="Err1")
{
document.write("Error! The value is too high.");
}
if(err=="Err2")
{
document.write("Error! The value is too low.");
}
if(err=="Err3")
{
document.write("Error! The value is not a number.");
}
}
</script>

Insert Special Characters - The backslash (\) is used to insert apostrophes, new lines, quotes, and other special characters into a text string.

var txt="We are the so-called \"Vikings\" from the north.";


Break up a Code Line
You can break up a code line within a text string with a backslash. The example below will be displayed properly:

document.write("Hello \
World!");However, you cannot break up a code line like this:

document.write \
("Hello World!");


Object Based Programming - JavaScript is an Object Based Programming language, and allows you to define your own objects and make your own variable types.

An object has properties and methods.
Properties are the values associated with an object.

In the following example we are using the length property of the String object to return the number of characters in a string:

<script type="text/javascript">
var txt="Hello World!";
document.write(txt.length);
</script>

Methods are the actions that can be performed on objects.

In the following example we are using the toUpperCase() method of the String object to display a text in uppercase letters:

<script type="text/javascript">
var str="Hello world!";
document.write(str.toUpperCase());
</script>


String object - The String object is used to manipulate a stored piece of text.

The following example uses the length property of the String object to find the length of a string:

var txt="Hello world!";
document.write(txt.length);

The following example uses the toUpperCase() method of the String object to convert a string to uppercase letters:

var txt="Hello world!";
document.write(txt.toUpperCase());

var str=document.getElementById("demo").innerHTML;
var n=str.replace("Microsoft","W3Schools");
document.getElementById("demo").innerHTML=n;

function myFunction()
{
var str="Hello world, welcome to the universe.";
var n=str.indexOf("welcome");
document.getElementById("demo").innerHTML=n;
}


Date Object - The Date object is used to work with dates and times.

There are four ways of initiating a date:

new Date() // current date and time
new Date(milliseconds) //milliseconds since 1970/01/01
new Date(dateString)
new Date(year, month, day, hours, minutes, seconds, milliseconds)

Use getFullYear() to get the year, as :
var d = new Date();
alert(d.getFullYear());

getTime() returns the number of milliseconds since 01.01.1970.
var d = new Date();
alert(d.getTime());

use setFullYear() to set a specific date.
var d = new Date();
d.setFullYear(2020,10,3);
This will set the date as :Tue Nov 3 11:31:49 UTC+0530 2020


Compare Two Dates
The Date object is also used to compare two dates.

The following example compares today's date with the 14th January 2100:

var x=new Date();
x.setFullYear(2100,0,14);
var today = new Date();

if (x>today)
{
alert("Today is before 14th January 2100");
}
else
{
alert("Today is after 14th January 2100");
}


Array Object - The Array object is used to store multiple values in a single variable.
An array can be defined in three ways.
1:
var myCars=new Array(); // regular array (add an optional integer
myCars[0]="Saab"; // argument to control array's size)
myCars[1]="Volvo";
myCars[2]="BMW";2:

var myCars=new Array("Saab","Volvo","BMW"); // condensed array3:

var myCars=["Saab","Volvo","BMW"]; // literal arrayNote: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.


You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.
document.write(myCars[0]);

Join two or more arrays with CONCAT():
var hege = ["Cecilie", "Lone"];
var stale = ["Emil", "Tobias", "Linus"];
var kai = ["Robin"];
var children = hege.concat(stale,kai);

Remove the last element of an array - pop()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.pop();

Add new elements to the end of an array - push()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.push("Kiwi")

Reverse the order of the elements in an array - reverse()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.reverse();
Then the order will be : Mango,Apple,Orange,Banana

Sort an array (alphabetically and ascending) - sort()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();

Sort numbers (numerically and ascending) - sort()
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});

Sort numbers (numerically and descending) - sort()
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a});

Add an element to position 2 in an array - splice()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");


Boolean Object - The Boolean object is used to convert a non-Boolean value to a Boolean value (true or false).
The following code creates a Boolean object called myBoolean:

var myBoolean=new Boolean();If the Boolean object has no initial value, or if the passed value is one of the following:

•0
•-0
•null
•""
•false
•undefined
•NaN
the object is set to false. For any other value it is set to true (even with the string "false")!


Math Object - The Math object allows you to perform mathematical tasks.
Syntax for using properties/methods of Math:

var x=Math.PI;
var y=Math.sqrt(16);
Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.

Constants: You may reference these constants from your JavaScript like this:

Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E

Mathematical Methods:
Math.round()
random()
max() example Math.max(5,10;
min()


RegExp Object - RegExp, is short for regular expression.
A regular expression is an object that describes a pattern of characters.

Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on text.

Syntax
var patt=new RegExp(pattern,modifiers);
or more simply:
var patt=/pattern/modifiers;
•pattern specifies the pattern of an expression
•modifiers specify if a search should be global, case-sensitive, etc.

The i modifier is used to perform case-insensitive matching.
The g modifier is used to perform a global match (find all matches rather than stopping after the first match).

Example 1
Do a case-insensitive search for "w3schools" in a string:
var str = "Visit W3Schools";
var patt1 = /w3schools/i;
document.write(str.match(patt1));

Result will be:
W3Schools

test()
The test() method searches a string for a specified value, and returns true or false, depending on the result.

The following example searches a string for the character "e":

Example
var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));Since there is an "e" in the string, the output of the code above will be:

true

exec()
The exec() method searches a string for a specified value, and returns the text of the found value. If no match is found, it returns null.

The following example searches a string for the character "e":

Example 1
var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));Since there is an "e" in the string, the output of the code above will be:
e


JavaScript Cookies - A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.
create a function that stores the name of the visitor in a cookie variable:

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

Friday, 24 August 2012

Interview Questions and Answers Unix & Shell

Q. How will you kill all the child process of a parent process?
For example a.sh is parent shell and it execute many other shell files then first need to kill all the child and then main shell. How can we do this in unix shell scripting?

Q. What is autosys? Why do we need to use autosys?
Ans - Autosys is a job scheduling software like Control - M and cron, with the help of autosys we can define the run time,day,date,week and which script or program needs to be run.
In real business, you need to fire jobs not just based on scheduled time, but also based on events (like an arrival of a file), and, based on the success or failure of other jobs. Autosys is a total job scheduling solution that allows you to define these events, dependencies, time schedules, alerts, etc, making it a complete data center automation tool.
The main advantage of using autosys w.r.t crontab is that it is has a Java front end too, so a person do not need to be a Unix champ to create or change a job in autosys.

Q. How to schedule a job in autosys?
Ans - There are the two methods you can use to create job definitions:
¦ Using the AutoSys Graphical User Interface (GUI).
¦ Using the AutoSys Job Information Language (JIL) through a command-line interface.

Q. Have you ever used any other scheduling tool, other than Cron Tab?
Ans – Yes I have used Autosys and Control M.

Q. How can you identify the process currently running?
Ans - ps -eaf | grep RU -- will list out the Processes currently running in the System

Q. Explain about sourcing commands?
Sourcing commands help you to execute the scripts within the scripts. For example sh command makes your program to run as a separate shell. .command makes your program to run within the shell. This is an important command for beginners and for special purposes.

Q. How do you read arguments in a shell program - $1, $2 ?
A. Shell script accepts parameters in following format...
$1 : first
$2 : second....so on upto
$9 : 9th param
whereas $0 : gives script/function name
If your script has more than 9 params then accept in following way...
${12} : 12th param
${18} : 18th param

Q. What are the different kinds of loops available in shell script?
A. Broadly categorised in 3
for
while
until
Q. What does $# stand for?
A. $# returns the number of parameters that are passed to a shell script
$? returns the exit code of the last executed command (0 : Successful, 1 or other: Failed)

Q. How do u open a read only file in Unix?
A. "vi -R filename"
In a shell script you can open a file in read only mode by using O_RDONLY
Syntax - open(<filename> O_RDONLY)
more <file>
cat <file>
less <file>

Q. What is the difference between a shell variable that is exported and the one that is not exported?
A. exported variable is visible to the child processes while the normal variables are not.
Export is to set the vaibales globally

Q. If you have a string "one two three", Which shell command would you use to extract the strings?
A. echo "one two three" | awk '{print $1 $2 $3}'
echo $string | cut -d -f1
echo $string | cut -d -f2
echo $string | cut -d -f3

Q. How do you schedule a command to run at 4:00 every morning?
A. Schedule the Job using crontab..From the command prompt perform a man on crontab and then type in crontab -e to create a new job
Crontab format
* * * * * <command>
<minute> <hour> <day> <month> <day_of_the_week> command
<0-59> <0-23> <1-31> <1-12> <0-7> command

Q. How will you list only the empty lines in a file (using grep)?
A. We can do this efficiently through awk script

awk '{
if (NF 0)
{
print "Here comes the empty line"
print $0
}
}' filename

NF number of fields in the current line
$0 current line

Q. How would you get the character positions 10-20 from a text file?
A. cat filename.txt | cut -c 10-20
TAB is default field separator for in a file for cut command.
cut -d"," -f2 cdr.txt
-d is for delimeter.
-f is for extracting the fields.
cut [OPTION]... [FILE]...
DESCRIPTION
Print selected parts of lines from each FILE to standard output.
Mandatory arguments to long options are mandatory for short options too.
-b, --bytes=LIST
output only these bytes
-c, --characters=LIST
output only these characters
-d, --delimiter=DELIM
use DELIM instead of TAB for field delimiter
-f, --fields=LIST
output only these fields; also print any line that contains no delimiter character, unless the -s option is specified.

Q. How would you print just the 25th line in a file (smallest possible script please)?
A. head -25 cdr.txt | tail -1
sed -n '25p' cdr.txt
using awk we can do the same also.

Q. How would you replace the n character in a file with some xyz?
A. sed 's/n/xyz/g' filename > new_filename

Q. What is chmod in unix?
A. This is for file permissions on unix- first is for self, second for group, and third for others.
And this rwx for each with 421.
Triplet for u: rwx => 4 + 2 + 1 = 7
Triplet for g: r-x => 4 + 0 + 1 = 5
Tripler for o: r-x => 4 + 0 + 1 = 5
Which makes : 755

UNIX chmod command
To change permissions on a UNIX file or directory, use the chmod command. chmod category+permissions filename category can be omitted, in which case the permissions specified are granted to all three categories (user, group and other). If + is replaced by a -, then those permissions are taken away from the categories.
As an example, consider this statement:
chmod o+r data
This grants other read permission to the file data. The command
chmod +x data
grants everyone (user, group and other) execute permission, and the command
chmod g+rwx data
gives category group read, write and execute permission.
Permissions change cumulatively, in that the commands
chmod +r file
chmod +w file
chmod +x file
are equivalent to
chmod +rwx file

Q. What is the difference between a 'thread' and a 'process'?
A. The key difference is that processes are fully isolated
from each other; threads share (heap) memory with other
threads running in the same application.
OR
A process is an instance of an running application.
And a Thread is the execution stream of the process.

A process can have multiple threads.
OR
Changes to the main thread (cancellation, priority
change, etc.) may affect the behavior of the other threads
of the process; changes to the parent process does not
affect child processes. If we consider running a word
processing program to be a process, then the auto-save and
spell check features that occur in the background are
different threads of that process which are all operating
on the same data set (your document).
OR
A process runs in it’s own memory area, where the thread always run in the parent process memory.
This is just like parent child.

Q. Write a shell script to identify the given string is palindrome or not?
A. var hai
var1 `echo $var|rev`
if [ $var ! $var1 ]
then
echo $var is not palindrome.
else
echo $var is palyndrome
fi

Q. How Connect to a Database in Shell Programming?Please tell me Step by Step?
A. Hi
In order to connect to a database using a shell script it is always necessary to set the environment first and then connect to the database and execute whatever the statements u need to and come out of the script gracefully.

ex:
#!/bin/sh
export ORACLE_SID $1
export ORACLE_HOME more /etc/oratab|grep -v '#'|grep -i $1|cut -d":" -f2
export LD_LIBRARY_PATH $ORACLE_HOME/lib:/usr/lib
export PATH $ORACLE_HOME/bin:$HOME/bin:$PATH

sqlplus -s sys/<passwd> as sysdba <EOF
select name from v$database;
archive log list;
exit
EOF

The above script echoes the name of the database you are connected along with the archive log mode information.

Q. What is this line in the shell script do ?#!/bin/ksh?
A. This line is called as "Hash Bang" Statement.
It gives the environment or the shell in which your script would be run

Q. What is the basic difference u find between a shell script and perl.I mean the advantages of one over the other?
A. 1) PERL scripts can be used on both UNIX and windows systems unless some OS specific commands are used. But the same case is not with Shell scripting.
2) PERL scripts are used for web based applications but shell scripts can not be used for the same purpose.
3) PERL modules gives PERL extra edge over Shell scripts. PERL modules are extensive and can be used for n number of purposes.
4) Data base drivers are available for perl also.

Q. How to take input values from the user?
A. 1. using read command
2. reading input from a file

Q. How to compress files by using shell scripting?
A.
Command Syntax File Produced
gzip gzip .gz
zip zip .zip
compress compress .Z
pack pack .z
Gzip, zip, compress, pack are used for compression of files.
Tar is also used to compress.

Q. how to delete a word from a file using shell scripting???
A. sed -e 's/word//g' filename

Q. How do you search the string for vowel's occurrence and number of occurrences of each vowel.
A. grep -io [aeiou] filename | wc –w

Q. You have current directory containing set of directories which contain files.
One file can reside in many directories.
Write script which returns number of unique file names in
all the subdirectories of a current dir.
A. ls -R|sort|uniq

Q. What are the different types of shells available in UNIX?
A. bourne (sh)
c shell (csh)
korn (ksh)
bourne again shell (bash)
TC shell (tcsh)

Q. What are the different security mechanisms available in UNIX?
A. Unix is having 3 ways of Security Mechanism:
1. By granting or revoking File permissions. Owner or Admin can change permissions to be given to group or others by using chmod command in Unix.
2. Login is restricted using login credentials ( User name and Password ).
3. Password is kept in encrypted format in the file /etc/passwd

Q. What are the type of files in unix?
A. File is of type c:
b block (buffered) special
c character (unbuffered) special
d directory
p named pipe (FIFO)
f regular file
l symbolic link
s socket
D door (Solaris)

Q. what is find command?
A. find is used for searching dir for files.
Examples –
find /oracle/oracle9i/Import -perm 755 -name "*.htm*" -mtime -1 -size 100k

Q. What is a named pipe?
A named pipe is a special file that is used to transfer data
between unrelated processes. One (or more) processes write to it, while
another process reads from it. Named pipes are visible in the file
system and may be viewed with
`ls' like any other file. (Named
pipes are also called
fifos; this term stands for `First In, First
Out'.)
To create a named pipe, use the Unix command mknod(1) or on some systems, mkfifo(1). These may not be in your normal path.
# system return val is backwards, so && not ||
#
$ENV{PATH} .= ":/etc:/usr/etc";
if ( system('mknod', $path, 'p')
&& system('mkfifo', $path) )
{
die "mk{nod,fifo} $path failed;
}
A fifo is convenient when you want to connect a process to an unrelated one. When you open a fifo, the program will block until there's something on the other end.
For example, let's say you'd like to have your .signature file be a named pipe that has a Perl program on the other end. Now every time any program (like a mailer, news reader, finger program, etc.) tries to read from that file, the reading program will block and your program will supply the new signature. We'll use the pipe-checking file test -p to find out whether anyone (or anything) has accidentally removed our fifo.
chdir; # go home
$FIFO = '.signature';
$ENV{PATH} .= ":/etc:/usr/games";
while (1) {
unless (-p $FIFO) {
unlink $FIFO;
system('mknod', $FIFO, 'p')
&& die "can't mknod $FIFO: $!";
}
# next line blocks until there's a reader
open (FIFO, "> $FIFO") || die "can't write $FIFO: $!";
print FIFO "John Smith (smith\@host.org)\n", `fortune -s`;
close FIFO;
sleep 2; # to avoid dup signals
}

Q. What is relative path and absolute path?
Absolute path : Exact path from root directory.
Relative path : Relative to the current path.

Q. Explain kill() and its possible return values?
There are four possible results from this call:
‘kill()’ returns 0. This implies that a process exists with the given PID, and the system would allow you to send signals to it. It is system-dependent whether the process could be a zombie.
‘kill()’ returns -1, ‘errno == ESRCH’ either no process exists with the given PID, or security enhancements are causing the system to deny its existence. (On some systems, the process could be a zombie.)
‘kill()’ returns -1, ‘errno == EPERM’ the system would not allow you to kill the specified process. This means that either the process exists (again, it could be a zombie) or draconian security enhancements are present (e.g. your process is not allowed to send signals to *anybody*).
‘kill()’ returns -1, with some other value of ‘errno’ you are in trouble! The most-used technique is to assume that success or failure with ‘EPERM’ implies that the process exists, and any other error implies that it doesn't.
An alternative exists, if you are writing specifically for a system (or all those systems) that provide a ‘/proc’ filesystem: checking for the existence of ‘/proc/PID’ may work.

Q. What is a pipe and give an example?
A pipe is two or more commands separated by pipe char '|'. That tells the shell to arrange for the output of the preceding command to be passed as input to the following command.
Example : ls -l | pr
The output for a command ls is the standard input of pr.
When a sequence of commands are combined using pipe, then it is called pipeline.

Q. How to terminate a process which is running and the specialty on command kill 0?
To find out weather a process is running or not :
We can use ps command with gives the current running process.
And after that we can apply grep command to filter out the process name.
Like to find out sshd process is running or not : $ ps -ewwo pid,args | grep [s]sh
Output :
5341 /usr/sbin/sshd
5864 /usr/bin/ssh-agent x-session-manager
6289 ssh oldbox
7126 ssh admin@core.r1.vsnl.router

Where
  • ps : Command name
  • -ewwo pid,args : -e option force to select all running processes. -o option is used to specify user-defined format. In our case we are forcing to display only program pid and its arguments. Finally -w option specifies wide output. Use this option twice for unlimited width.
  • grep [s]sh : We are just filtering out sshd string
Once we got the process id we can kill the process with kill command:
Syntax is : kill -N PID
Where,
  • N is a signal number
  • PID is the Process Identification Number. If you do not know the PID, it can be learned through the ps command.

Some of the more commonly used signals:

signal #
Usage
1
HUP (hang up) (Default)
2
INT (interrupt)
3
QUIT (quit)
6
ABRT (abort)
9
KILL (non-catchable, non-ignorable kill)
14
ALRM (alarm clock)
15
TERM (software termination signal)

Kill 0 - kills all processes in your system except the login shell.

Q. What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example : ls > wc

Q. What are shell variables?
Shell variables are special variables, a name-value pair created and maintained by the shell.
Example: PATH, HOME, MAIL and TERM

Q. How to switch to a super user status to gain privileges?
Use ‘su’ command. The system asks for password and when valid entry is made the user gains super user (admin) privileges.

Q. How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.

Q. How many prompts are available in a UNIX system?
Two prompts, PS1 (Primary Prompt), PS2 (Secondary Prompt).

Q. Name the data structure used to maintain file identification?
‘inode’, each file has a separate inode and a unique inode number.

Q. Is it possible to count number char, line in a file; if so, How?
Yes, wc-stands for word count.
wc -c for counting number of characters in a file.
wc -l for counting lines in a file.

Q. Is ‘du’ a command? If so, what is its use?
Yes, it stands for ‘disk usage’. With the help of this command you can find the disk capacity and free space of the disk.

Q. What is the use of the command "ls -x chapter[1-5]"?
ls stands for list; so it displays the list of the files that starts with 'chapter' with suffix '1' to '5', chapter1, chapter2, and so on.

Q. Is it possible to restrict incoming message?
Yes, using the ‘mesg’ command.

Q. Is it possible to create new a file system in UNIX?
Yes, ‘mkfs’ is used to create a new file system.

Q. What will the following command do?
$ echo *
It is similar to 'ls' command and displays all the files in the current
directory.

Q. Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.

Q. Which command is used to delete all files in the current directory and all its sub-directories?
rm -r *

Q. Write a command to kill the last background job?
Kill $!

Q. What is the difference between cat and more command?
Cat displays file contents. If the file is large the contents scroll off the screen before we view it. So command 'more' is like a pager which displays the contents page by page.


Q. What is the use of ‘grep’ command?
‘grep’ is a pattern search command. It searches for the pattern, specified in the command line with appropriate option, in a file(s).
Syntax : grep
Example : grep 99mx mcafile

Q. What difference between cmp and diff commands?
cmp - Compares two files byte by byte and displays the first mismatch
diff - tells the changes to be made to make the files identical

Q. Explain the steps that a shell follows while processing a command?
After the command line is terminated by the key, the shel goes ahead with processing the command line in one or more passes. The sequence is well defined and assumes the following order.
Parsing: The shell first breaks up the command line into words, using spaces and the delimiters, unless quoted. All consecutive occurrences of a space or tab are replaced here with a single space.
Variable evaluation: All words preceded by a $ are avaluated as variables, unless quoted or escaped.
Command substitution: Any command surrounded by backquotes is executed by the shell which then replaces the standard output of the command into the command line.
Wild-card interpretation: The shell finally scans the command line for wild-cards (the characters *, ?, [, ]). Any word containing a wild-card is replaced by a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to the command.
PATH evaluation: It finally looks for the PATH variable to determine the sequence of directories it has to search in order to hunt for the command.

Q. What is the difference between > and >> redirection operators ?
is the output redirection operator when used it overwrites while >> operator appends into the file.

Q. Which of the following commands is not a filter?(a)man , (b) cat , (c) pg , (d) head?
Ans: man
A filter is a program which can receive a flow of data from std input, process (or filter) it and send the result to the std output.


Q. Construct pipes to execute the following jobs?
1. Output of who should be displayed on the screen with value of total number of users who have logged in displayed at the bottom of the list.
2. Output of ls should be displayed on the screen and from this output the lines containing the word ‘poem’ should be counted and the count should be stored in a file.
3. Contents of file1 and file2 should be displayed on the screen and this output should be appended in a file .
From output of ls the lines containing ‘poem’ should be displayed on the screen along with the count.
4. Name of cities should be accepted from the keyboard . This list should be combined with the list present in a file. This combined list should be sorted and the sorted list should be stored in a file ‘newcity’.
5. All files present in a directory dir1 should be deleted any error while deleting should be stored in a file ‘errorlog’.

Q. How to find free space in unix/linux?
df -h

Q. What is the difference between soft link and hard link in unix operating system ?
Hard Links :
1. All Links have same inode number.
2.ls -l command shows all the links with the link column(Second) shows No. of links.
3. Links have actual file contents
4.Removing any link just reduces the link count but doesn't affect other links.

Soft Links(Symbolic Links) :
1.Links have different inode numbers.
2. ls -l command shows all links with second column value 1 and the link points to original file.
3. Link has the path for original file and not the contents.
4.Removing soft link doesn't affect anything but removing original file the link becomes dangling link which points to nonexistant file.

Q. What are the differences between Shared and Dynamic libraries?
Shared libraries are loaded into memory before compilation during parsing whereas dynamic libraries are loaded during compilation time itself.
Q. How would you create shared and dynamic libraries?
Well shared libraries have 2 types

1) Static
2) Dynamic.
u can create library by
ar cr -o sharedobj.a file1.o file2.o
while file1 and file2 are headfiles (obj)
now put this sharedobj.a into /usr/lib directory

Q. What type of scheduling is used in Unix?
Multi Level Feedback Queue Scheduling with each queue in round robin

Q. What is stty used for?
stty is used to set your terminal options. Following are some of the typical examples1. stty echo -iuclc The above command will switch off character display on your screen and ignore any case differences (ignore upper case and lower case). This can be used in situation where password is read and should not get printed on screen2. stty -erase '^H' This will set the erase character for your terminal3. stty -intr '^c' This will set the execution interrupt as control+c key 4. stty -eof '^d' This will set the end of file character as control+d key

Q. How to read and write of a open file?
Read from file:
cat file_name

Or on terminal pagewise read as :
more file_name.
Write to file :
echo "`cmd`" > file_name.

Where cmd= any executable UNIX command.
> = redirect the o/p.
>> = append to file_name.

Q. What are the directory commands available in UNIX programming?
mkdir to create directory
cd to change directory
rmdir to remove directory (must be empty)
pwd to show present working directory
ls to list contents of a directory

Q. What is the significance of the “tee” command?
It reads the standard input and sends it to the standard output while redirecting a copy of what it has read to
the file specified by the user.

Q. Write a command to display a file’s contents in various formats?
$od -cbd file_name
c - character, b - binary (octal), d-decimal, od=Octal Dump.
Q. How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.

Q. In Unix, what is a symbolic link, and how do I create one?

A symbolic link, also termed a soft link, is a special kind of file that points to another file, much like a shortcut in Windows or a Macintosh alias. Unlike a hard link, a symbolic link does not contain the data in the target file. It simply points to another entry somewhere in the file system. This difference gives symbolic links certain qualities that hard links do not have, such as the ability to link to directories, or to files on remote computers networked through NFS. Also, when you delete a target file, symbolic links to that file become unusable, whereas hard links preserve the contents of the file.
To create a symbolic link in Unix, at the Unix prompt, enter:
ln -s source_file myfile
Replace source_file with the name of the existing file for which you want to create the symbolic link (this file can be any existing file or directory across the file systems). Replace myfile with the name of the symbolic link. The ln command then creates the symbolic link. After you've made the symbolic link, you can perform an operation on or execute myfile, just as you could with the source_file. You can use normal file management commands (e.g., cp, rm) on the symbolic link.
Note: If you delete the source file or move it to a different location, your symbolic file will not function properly. You should either delete or move it. If you try to use it for other purposes (e.g., if you try to edit or execute it), the system will send a "file nonexistent" message.

Q. Where is Socket used?

A Unix Socket is used in a client server application frameworks. A server is a process which does some function on request from a client. Most of the application level protocols like FTP, SMTP and POP3 make use of Sockets to establish connection between client and server and then for exchanging data.

Socket Types:

There are four types of sockets available to the users. The first two are most commenly used and last two are rarely used.
  • Stream Sockets: Delivery in a networked environment is guaranteed. If you send through the stream socket three items "A,B,C", they will arrive in the same order - "A,B,C". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do no have any boundaries.
  • Datagram Sockets: Delivery in a networked environment is not guaranteed. They're connectionless because you don't need to have an open connection as in Stream Sockets - you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).
  • Raw Sockets: provides users access to the underlying communication protocols which support socket abstractions. These sockets are normally datagram oriented, though their exact characteristics are dependent on the interface provided by the protocol. Raw sockets are not intended for the general user; they have been provided mainly for those interested in developing new communication protocols, or for gaining access to some of the more esoteric facilities of an existing protocol.
  • Sequenced Packet Sockets: They are similar to a stream socket, with the exception that record boundaries are preserved. This interface is provided only as part of the Network Systems (NS) socket abstraction, and is very important in most serious NS applications. Sequenced-packet sockets allow the user to manipulate the Sequence Packet Protocol (SPP) or Internet Datagram Protocol (IDP) headers on a packet or a group of packets either by writing a prototype header along with whatever data is to be sent, or by specifying a default header to be used with all outgoing data, and allows the user to receive the headers on incoming packets.
Q. What is door in unix?
Ans. - Doors are an inter-process communication facility for Unix computer systems. They provide a form of procedure call. Doors were developed by Sun Microsystems as a core part of the Spring operating system, then added to Solaris
Crontab – Quick Reference

Setting up cron jobs in Unix and Solaris
cron is a unix, solaris utility that b which tasks run automatically in the background at regular intervals. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity

1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use crontab if your name does not appear in the file /usr/lib/cron/cron.deny. If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab.

2. Crontab Commands
export EDITOR=vi ;to specify a editor to open crontab file.
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)

3. Crontab file
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)

* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).

4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30 18 * * * rm /home/someuser/tmp/*
Changing the parameter values as below will cause this command to run at different time schedule below :

min
hour
day/month
month
day/week
Execution time
30
0
1
1,6,12
*
00:30 Hrs on 1st of Jan, June & Dec.

0
20
*
10
1-5
–8.00 PM every weekday (Mon-Fri) only in Oct.

0
0
1,10,15
*
*
– midnight on 1st ,10th & 15th of month

5,10
0
10
*
1
– At 12.05,12.10 every Monday & on 10th of every month
:
Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
7. Generate log file
To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Basic UNIX commands

Files

Ls command
ls command is most widely used command and it displays the contents of directory.
options
ls will list all the files in your home directory, this command has many options.
ls -l will list all the file names, permissions, group, etc in long format.
ls -a will list all the files including hidden files that start with . .
ls -lt will list all files names based on the time of creation, newer files bring first.
ls -Fxwill list files and directory names will be followed by slash.
ls -Rwill lists all the files and files in the all the directories, recursively.
ls -R | more will list all the files and files in all the directories, one page at a time.
  • more filename --- shows the first part of a file, that can fit on one screen. Just hit the space bar to see more or q to quit.
  • emacs filename --- is an editor that lets you create and edit a file.
  • mv filename1 filename2 --- moves a file
  • cp filename1 filename2 --- copies a file
  • rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything.
  • diff filename1 filename2 --- compares files, and shows where they differ
  • wc filename --- tells you how many lines, words, and characters there are in a file -l for lines, -c for char, -w for words.
  • chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r filename will make it unreadable for others again.
  • File Compression
    • gzip filename --- compresses files. Usually text files compress to about half their original size, but it depends very much on the size of the file and the nature of the contents. There are other tools for this purpose, too (e.g. compress), but gzip usually gives the highest compression rate. Gzip produces files with the ending '.gz' appended to the original filename.
    • gunzip filename --- uncompresses files compressed by gzip.
    • gzcat filename --- lets you look at a gzipped file without actually having to gunzip it (same as gunzip -c). You can even print it directly, using gzcat filename | lpr
  • printing
    • lpr filename --- print. Use the -P option to specify the printer name if you want to use a printer other than your default printer. For example, if you want to print double-sided, use 'lpr -Pvalkyr-d', or if you're at CSLI, you may want to use 'lpr -Pcord115-d'. See 'help printers' for more information about printers and their locations.
    • lpq --- check out the printer queue, e.g. to get the number needed for removal, or to see how many other files will be printed before yours will come out
    • lprm jobnumber --- remove something from the printer queue. You can find the job number by using lpq. Theoretically you also have to specify a printer name, but this isn't necessary as long as you use your default printer in the department.
    • genscript --- converts plain text files into postscript for printing, and gives you some options for formatting. Consider making an alias like alias ecop 'genscript -2 -r \!* | lpr -h -Pvalkyr' to print two pages on one piece of paper.
    • dvips filename --- print .dvi files (i.e. files produced by LaTeX). You can use dviselect to print only selected pages. See the LaTeX page for more information about how to save paper when printing drafts.

Directories

Directories, like folders on a Macintosh, are used to group files together in a hierarchical structure.
  • mkdir dirname --- make a new directory
  • cd dirname --- change directory. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position.
  • pwd --- tells you where you currently are.

About other people

  • w --- tells you who's logged in, and what they're doing. Especially useful: the 'idle' part. This allows you to see whether they're actually sitting there typing away at their keyboards right at the moment.
$w
11:47:16 up 28 days, 8:22, 2 users, load average: 1.01, 1.03, 1.26
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
whdcaxkc pts/0 10.57.42.146 Thu17 20:11m 0.35s 0.01s login -- whdcaxkc
whdcaxkc pts/1 10.57.12.174 10:38 0.00s 0.13s 0.01s login -- whdcaxkc
  • who --- tells you who's logged on, and where they're coming from (IP add).
$who
whdcaxkc pts/0 Aug 5 17:05 (10.57.42.146)
whdcaxkc pts/1 Aug 9 10:38 (10.57.12.174)
  • finger username --- gives you lots of information about that user, e.g. when they last read their mail and whether they're logged in. Often people put other practical information, such as phone numbers and addresses, in a file called .plan. This information is also displayed by 'finger'.
$finger whdcaxkc
Login: whdcaxkc Name: Kannan Chandran
Directory: /home/whdcaxkc Shell: /bin/bash
On since Thu Aug 5 17:05 (IST) on pts/0 from 10.57.42.146
20 hours 7 minutes idle
On since Mon Aug 9 10:38 (IST) on pts/1 from 10.57.12.174
No mail.
No Plan.
  • last -1 username --- tells you when the user last logged on and off and from where. Without any options, last will give you a list of everyone's logins.
  • talk username --- lets you have a (typed) conversation with another user
  • write username --- lets you exchange one-line messages with another user
  • elm --- lets you send e-mail messages to people around the world (and, of course, read them). It's not the only mailer you can use, but the one we recommend. It is like mailx on sun.

About your (electronic) self

  • whoami --- returns your username. Sounds useless, but isn't. You may need to find out who it is who forgot to log out somewhere, and make sure *you* have logged out.
  • finger & .plan files
    of course you can finger yourself, too. That can be useful e.g. as a quick check whether you got new mail. Try to create a useful .plan file soon. Look at other people's .plan files for ideas. The file needs to be readable for everyone in order to be visible through 'finger'. Do 'chmod a+r .plan' if necessary. You should realize that this information is accessible from anywhere in the world, not just to other people on turing.
  • passwd --- lets you change your password, which you should do regularly (at least once a year).
  • ps -u yourusername --- lists your processes. Contains lots of information about them, including the process ID, which you need if you have to kill a process.
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
whdcaxkc 4254 0.0 0.0 3480 744 pts/1 R+ 11:56 0:00 ps -u
whdcaxkc 9393 0.0 0.0 6004 1480 pts/0 S+ Aug06 0:00 bash
whdcaxkc 21998 0.0 0.0 5000 1508 pts/0 Ss Aug05 0:00 -bash
whdcaxkc 27731 0.0 0.0 5628 1488 pts/1 Ss 10:38 0:00 -bash
whdcaxkc 31295 0.0 0.0 4012 856 pts/1 T 11:08 0:00 man find
  • kill PID --- kills (ends) the processes with the ID you gave. This works only for your own processes, of course. Get the ID by using ps. If the process doesn't 'die' properly, we can use the option -9. But we should attempt without that option first, because it doesn't give the process a chance to finish possibly important business before dying.
  • quota -v --- show what your disk quota is (i.e. how much space you have to store files), how much you're actually using, and in case you've exceeded your quota (which you'll be given an automatic warning about by the system) how much time you have left to sort them out (by deleting or gzipping some, or moving them to your own computer).
  • du filename --- shows the disk usage of the files and directories in filename (without argument the current directory is used). du -s gives only a total.
  • last yourusername --- lists your last logins. Can be a useful memory aid for when you were where, how long you've been working for.

Connecting to the outside world

  • nn --- allows you to read news. It will first let you read the news local to turing, and then the remote news. If you want to read only the local or remote news, you can use nnl or nnr, respectively.
  • rlogin hostname --- lets you connect to a remote host
  • telnet hostname --- also lets you connect to a remote host. Use rlogin whenever possible.
  • ftp hostname --- lets you download files from a remote host which is set up as an ftp-server. The most important commands within ftp are get for getting files from the remote machine, and put for putting them there (mget and mput let you specify more than one file at once). Be sure not to confuse the two, especially when your physical location doesn't correspond to the direction of the ftp connection you're making. ftp just overwrites files with the same filename. If you're transferring anything other than ASCII text, use binary mode.
  • lynx --- lets you browse the web from an ordinary terminal.

Miscellaneous tools

  • date --- shows the current date and time.
  • cal --- shows a calendar of the current month. Use e.g., 'cal 10 1995' to get that for October 95, or 'cal 1995' to get the whole year.


Nohup command.
nohup command if added in front of any command will continue running the command or process even if you shut down your terminal or close your session to machine. For example, if I want to run a job that takes lot of time and must be run from terminal and is called update_entries_tonight .


Ln command.
Instead of copying you can also make links to existing files using ln command.
If you want to create a link to a file called coolfile in /usr/local/bin directory then you can enter this command.
ln mycoolfile /usr/local/bin/coolfile
Some examples:
ln -s fileone filetwo will create a symbolic link and can exist across machines.
ln -n option will not overwrite existing files.
ln -f will force the link to occur.

Diff command.
diff command will compare the two files and print out the differences between.

Cmp command.
cmp command compares the two files. For exmaple I have two different files fileone and filetwo.
cmp fileone filetwo will give me
fileone filetwo differ: char 80, line 4
if I run cmp command on similar files nothing is returned.
-s command can be used to return exit codes. i.e. return 0 if files are identical, 1 if files are different, 2 if files are inaccessible.
This following command prints a message 'no changes' if files are same
cmp -s fileone file1 && echo 'no changes' .

Find command.
Find command is a extremely useful command. you can search for any file anywhere using this command provided that file and directory you are searching has read write attributes set to you ,your, group or all. Find descends directory tree beginning at each pathname and finds the files that meet the specified conditions. Here are some examples.

Some Examples:
find $HOME -print will lists all files in your home directory.
find /work -name chapter1 -print will list all files named chapter1 in /work directory.
find / -type d -name 'man*' -print will list all manpage directories.
find / -size 0 -ok rm {} \; will remove all empty files on system.
conditions of find
-atime +n |-n| n will find files that were last accessed more than n or less than -n days or n days.
-ctime +n or -n will find that were changed +n -n or n days ago.
-depth descend the directory structure, working on actual files first and then directories. You can use it with cpio command.
-exec commad {} \; run the Unix command on each file matched by find. Very useful condition.
-print print or list to standard output (screen).
-name pattern find the pattern.
-perm nnnfind files whole permission flags match octal number nnn.
-size n find files that contain n blocks.
-type c Find file whole type is c. C could be b or block, c Character special file, d directory, p fifo or named pipe, l symbolic link, or f plain file.

Sort command.
sort command sort the lines of a file or files, in alphabetical order. for example if you have a file named testfile with these contents
zzz
aaa
1234
yuer
wer
qww
wwe
Then running
sort testfile
will give us output of
1234
aaa
qww
wer
wwe
yuer
zzz
Options:
-b ignores leading spaces and tabs.
-c checks whether files are already sorted.
-d ignores punctuation.
-i ignores non-printing characters.
-n sorts in arithmetic order.
-ofile put output in a file.
+m[-m] skips n fields before sorting, and sort upto field position m.
-r reverse the order of sort.
-u identical lines in input file apear only one time in output.

Uniq command.
uniq command removes duplicate adjacent lines from sorted file while sending one copy of each second file.
Examples

sort names | uniq -d will show which lines appear more than once in names file.
Options:
-c print each line once, counting instances of each.
-d print duplicate lines once, but no unique lines.
-u print only unique lines.

Sed command.
sed command launches a stream line editor which you can use at command line.
you can enter your sed commands in a file and then using -f option edit your text file. It works as
sed [options] files
options:
-e 'instruction' Apply the editing instruction to the files.
-f script Apply the set of instructions from the editing script.
-n suppress default output.

Df command.
df command displays information about mounted filesystems. It reports the number of free disk blocks. Typically a Disk block is 512 bytes (or 1/2 Kilobyte).
syntax is
df options name
Options
-b will print only the number of free blocks.
-e will print only the number of free files.
-f will report free blocks but not free inodes.
-F type will report on an umounted file system specified by type.
-k will print allocation in kilobytes.
-l will report only on local file systems.
-n will print only the file system name type, with no arguments it lists type of all filesystems

Du command.
du command displays disk usage.

Touch – is used to change a file's access and modification timestamps. It is also used to create a new empty file.
-a, change the access time only
-c, if the file does not exist, do not create it and do not report this condition
-m, change the modification time only
-r file, use the access and modification times of file
-t time, use the time specified (in the format below) to update the access and modification times
The simplest use case for touch is thus:
# touch myfile.txt

Alias - It is mainly used for abbreviating a system command, or for adding default arguments to a regularly used command. An alias will last for the life of the shell session. Regularly used aliases can be set from the shell's configuration so that they will be available upon the start of the corresponding shell session.
Syntax
alias [name=['command']]
An example of the Bash shell syntax is:
alias copy="cp"
To view defined aliases the following commands can be used:
alias # Used without arguments; displays a list of all current aliases
alias -p # Analogous to the above; not available in 4DOS/4NT and PowerShell
alias myAlias # Displays the command for a defined alias
In Unix shells, if an alias exists for a command, it is possible to override the alias by surrounding the command with quotes or prefixing it with a backslash. For example, consider the following alias definition:
alias ls='ls -la'
To override this alias and execute the ls command as it was originally defined, the following syntax can be used:
'ls' or \ls
In Unix shells and 4DOS/4NT, aliases can be removed by executing the unalias command:
unalias copy # Removes the copy alias
unalias -a # The -a switch will remove all aliases; not available in 4DOS/4NT
unalias * # 4DOS/4NT equivalent of `unalias -a` - wildcards are supported

Tee - tee is normally used to split the output of a program so that it can be seen on the display and also be saved in a file. The command can also be used to capture intermediate output before the data is altered by another command or program. The tee command reads standard input, then writes its content to standard output and simultaneously copies it into the specified file(s) or variables.

The grep command allows you to search one file or multiple files for lines that contain a pattern. Exit status is 0 if matches were found, 1 if no matches were found, and 2 if errors occurred.
The syntax for the grep command is:
grep [options] pattern [files]
options:
-b
Display the block number at the beginning of each line.
-c
Display the number of matched lines.
-h
Display the matched lines, but do not display the filenames.
-i
Ignore case sensitivity.
-l
Display the filenames, but do not display the matched lines.
-n
Display the matched lines and their line numbers.
-s
Silent mode.
-v
Display all lines that do NOT match.
-w
Match whole word.
Examples:
grep -c tech file1

Some one Liners of Unix
  1. How do you find out what’s your shell? - echo $SHELL
  2. What’s the command to find out today’s date? - date
  3. What’s the command to find out users on the system? - who
  4. How do you find out the current directory you’re in? - pwd
  5. How do you remove a file? - rm
  6. How do you find out your own username? - whoami
  7. How do you count words, lines and characters in a file? – wc –l or –w or -c
  8. How do you search for a string inside a given file? - grep string filename
  9. How do you search for a string inside a directory? - grep string *
  10. How do you search for a string in a directory with the subdirectories recursed? - grep -r string *
  11. What are PIDs? - They are process IDs given to processes. A PID can vary from 0 to 65535.
  12. How do you list currently running process? - ps
  13. How do you stop a process? - kill pid
  14. How do you find out about all running processes? - ps –ae : a for all user process, e for system process
  15. How do you stop all the processes, except the shell window? - kill 0
  16. How do you fire a process in the background? - ./process-name &
  17. How do you refer to the arguments passed to a shell script? - $1, $2 and so on. $0 is your script name.
  18. What’s the conditional statement in shell scripting? - if {condition} then … fi
  19. How do you do number comparison in shell scripts? - -eq, -ne, -lt, -le, -gt, -ge
  20. How do you test for file properties in shell scripts? - -s filename tells you if the file is not empty, -f filename tells you whether the argument is a file, and not a directory, -d filename tests if the argument is a directory, and not a file, -w filename tests for writeability, -r filename tests for readability, -x filename tests for executability
  21. How do you do Boolean logic operators in shell scripting? - ! tests for logical not, -a tests for logical and, and -o tests for logical or.
  22. How do you find out the number of arguments passed to the shell script? - $#
  23. What’s a way to do multilevel if-else’s in shell scripting? - if {condition} then {statement} elif {condition} {statement} fi
  24. How do you write a for loop in shell? - for {variable name} in {list} do {statement} done
  25. How do you write a while loop in shell? - while {condition} do {statement} done
  26. How does a case statement look in shell scripts? - case {variable} in {possible-value-1}) {statement};; {possible-value-2}) {statement};; esac
  27. How do you read keyboard input in shell scripts? - read {variable-name}
  28. How do you define a function in a shell script? - function-name() { #some code here return }
  29. How does getopts command work? - The parameters to your script can be passed as -n 15 -x 20. Inside the script, you can iterate through the getopts array as while getopts n:x option, and the variable $option contains the value of the entered option.
Q. State the advantages of Shell scripting?
There are many advantages of shell scripting some of them are, one can develop their own operating system with relevant features best suited to their organization than to rely on costly operating systems. Software applications can be designed according to their platform.
Q. What are the disadvantages of shell scripting?
There are many disadvantages of shell scripting they are
* Design flaws can destroy the entire process and could prove a costly error.
* Typing errors during the creation can delete the entire data as well as partition data.
* Initially process is slow but can be improved.
* Portbility between different operating system is a prime concern as it is very difficult to port scripts etc.
Q. Explain about the slow execution speed of shells?
Major disadvantage of using shell scripting is slow execution of the scripts. This is because for every command a new process needs to be started. This slow down can be resolved by using pipeline and filter commands.
Q. Give some situations where typing error can destroy a program?
There are many situations where typing errors can prove to be a real costly effort. For example a single extra space can convert the functionality of the program from deleting the sub directories to files deletion. cp, cn, cd all resemble the same but their actual functioning is different. Misdirected > can delete your data.
Q. Explain about Stdin, Stdout and Stderr?
These are known as standard input, output and error. These are categorized as 0, 1 and 2. Each of these functions has a particular role and should accordingly functions for efficient output. Any mismatch among these three could result in a major failure of the shell.
Q. Explain about sourcing commands?
Sourcing commands help you to execute the scripts within the scripts. For example sh command makes your program to run as a separate shell. .command makes your program to run within the shell.
Q. Explain about Login shell?
Login shell creates an environment and default parameters. It consists of two files they are profile files and shell rc files. These files initialize the login and non login files. Environment variables are created by Login shell.
Q. Explain about non-login shell files?
The non login shell files are initialized at the start and they are made to run to set up variables. Parameters and path can be set etc are some important functions. These files can be changed and also your own environment can be set. These functions are present in the root. It runs the profile each time you start the process.
Q. Explore about Environment variables?
Environment variables are set at the login time and every shell that starts from this shell gets a copy of the variable. When we export the variable it changes from an shell variable to an environment variable and these variables are initiated at the start of the shell.
Q. What type of variables are in shell?
There are two types of variable:
(1) System variables - Created and maintained by Linux itself. This type of variable defined in CAPITAL LETTERS.
(2) User defined variables (UDV) - Created and maintained by user.

System Variable
Meaning
BASH=/bin/bash
Our shell name
BASH_VERSION=1.14.7(1)
Our shell version name
COLUMNS=80
No. of columns for our screen
HOME=/home/vivek
Our home directory
LINES=25
No. of columns for our screen
LOGNAME=students
students Our logging name
OSTYPE=Linux
Our Os type
PATH=/usr/bin:/sbin:/bin:/usr/sbin
Our path settings
PS1=[\u@\h \W]\$
Our prompt settings
PWD=/home/students/Common
Our current working directory
SHELL=/bin/bash
Our shell name
USERNAME=vivek
User name who is currently login to this PC

Syntax:
variable name=value
Example:
$ n=10
(1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character.
(2) Variables are case-sensitive, just like filename in Linux.
(4) You can define NULL variable as follows (NULL variable is variable which has no value at the time of definition) For e.g.
$ vech=
$ vech=""

To print or access UDV use following syntax
Syntax:
$variablename
Define variable vech as follows:
$ vech=Bus
To print contains of variable 'vech' type
$ echo $vech
Q. How to perform arithmetic in shell?
Use to perform arithmetic operations.
Syntax:
expr op1 math-operator op2

Examples:
$ echo `expr 6 + 3`
For the last statement not the following points
(1) First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. Back quote is generally found on the key under tilde (~) on PC keyboard OR to the above of TAB key.
(2) Second, expr is also end with ` i.e. back quote.
(3) Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum
(4) Here if you use double quote or single quote, it will NOT work
Q. How to read values from user?
Syntax:
read variable1, variable2,...variableN
Example:
echo "Your first name please:"
read fname
echo "Hello $fname, Lets be friend!"

Q. How can we write more than one command in one line?
Syntax:
command1;command2
Examples:
$ date;who
Will print today's date followed by users who are currently login. Note that You can't use

Q. test command or [expr]?

test command or [ expr ] is used to see if an expression is true, and if it is true it return zero(0), otherwise returns nonzero for false.
Syntax:
test expression OR [ expression ]

Example:
#!/bin/sh
if test $1 -gt 0
then
echo "$1 number is positive"
fi

Shell also test for file and directory types
Test
Meaning
-s file
Non empty file
-f file
Is File exist or normal file and not a directory
-d dir
Is Directory exist and not a file
-w file
Is writeable file
-r file
Is read-only file
-x file
Is file is executable

Q. if else syntax ;
if condition
then
condition is zero (true - 0)
execute all commands up to else statement
elif

else
if condition is not true then
execute all commands up to fi
fi

Example :
$ vi isnump_n
#!/bin/sh
#
# Script to see whether argument is positive or negative
#
if [ $# -eq 0 ]
then
echo "$0 : You must give/supply one integers"
exit 1
fi

if test $1 -gt 0
then
echo "$1 number is positive"
else
echo "$1 number is negative"
fi

Q. for loop in shell?
Syntax:
for { variable name } in { list }
do
execute one for each item in the list until the list is
not finished (And repeat all statement between do and done)
done

Example:
for i in 1 2 3 4 5
do
echo "Welcome $i times"
done

Even you can use following syntax:
Syntax:
for (( expr1; expr2; expr3 ))
do
.....
...
repeat all statements between do and
done until expr2 is TRUE
Done

Example:
for (( i = 0 ; i <= 5; i++ ))
do
echo "Welcome $i times"
done

Q. While loop in shell?
Syntax:
while [ condition ]
do
command1
command2
command3
..
....
Done
Example:
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done

Q. How to debug shell script?
For this purpose you can use -v and -x option with sh or bash command to debug the shell script. General syntax is as follows:
Syntax:
sh option { shell-script-name }
OR
bash option { shell-script-name }
Option can be
-v Print shell input lines as they are read.
-x After expanding each simple-command, bash displays the expanded value of PS4 system variable, followed by the command and its expanded arguments.

Q. What is test command in unix shell?
The unix test command can test for various conditions, and then returns 0 for true and 1 for false. Usually, test is used in if...then or while loops.

Let start with a simple unix test or if examples for comparing two number equal or not
if [ 9 -eq 9 ]
then
echo equal
else
echo no equal
fi

We can do the same thing by using test command as
test 9 -eq 9
if [ $? -eq 0 ]
then
echo equal
else
echo no equal
fi

Q. What is /dev/null in unix?

This is special Linux file which is used to send any unwanted output from program/command.
Syntax:
command > /dev/null
Example:
$ ls > /dev/null
Output of above command is not shown on screen its send to this special file. The /dev directory contains other device files. The files in this directory mostly represent peripheral devices such disks like floppy disk, sound card, line printers etc
Q. What is conditional execution i.e. && and || in shell?

The control operators are && (read as AND) and || (read as OR). The syntax for AND list is as follows
Syntax:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.
The syntax for OR list as follows
Syntax:
command1 || command2
command2 is executed if and only if command1 returns a non-zero exit status.

You can use both as follows
Syntax:
command1 && comamnd2 if exist status is zero || command3 if exit status is non-zero
if command1 is executed successfully then shell will run command2 and if command1 is not successful then command3 is executed.
Example:
$ rm myf && echo "File is removed successfully" || echo "File is not removed"
If file (myf) is removed successful (exist status is zero) then "echo File is removed successfully" statement is executed, otherwise "echo File is not removed" statement is executed (since exist status is non-zero)
Q. how to write functions in shell?

Syntax:
function-name ( )
{
command1
command2
.....
...
commandN
return
}
Where function-name is name of you function, that executes series of commands. A return statement will terminate the function. Example:
Type SayHello() at $ prompt as follows
$ SayHello()
{
echo "Hello $LOGNAME, Have nice computing"
return
}

Arithmetic Operations in Shell Scripting

For arithmetic operations such as addition, subtraction, multiplication, and division. We uses a function called expr; e.g., "expr a + b" means 'add a and b'.
e.g.:
sum=`expr 12 + 20`
Similar syntax can be used for subtraction, division, and multiplication.
There is another way to handle arithmetic operations; enclose the variables and the equation inside a square-bracket expression starting with a "$" sign. The syntax is
$[expression operation statement]
e.g.:
echo $[12 + 10]

Conditional Statements

The syntax is show below:
if [ conditional statement ]
then
... Any commands/statements ...
fi

The script cited below will prompt for a username, and if the user name is "blessen", will display a message showing that I have successfully logged in. Otherwise it will display the message "wrong username".
#!/bin/sh
echo "Enter your username:"
read username
if [ "$username" = "blessen" ]
then
echo 'Success!!! You are now logged in.'
else
echo 'Sorry, wrong username.'
Fi

Remember to always enclose the variable being tested in double quotes; not doing so will cause your script to fail due to incorrect syntax when the variable is empty. Also, the square brackets (which are an alias for the 'test' command) must have a space following the opening bracket and preceding the closing one.

Variable Comparison

If the values of variables to be compared are numerical, then you have to use these options:
-eq Equal to
-ne Not Equal to
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater then or equal to

If they are strings, then you have to use these options:
= Equal to
!= Not Equal to
< First string sorts before second
> First string sorts after second

Loops

The "for" Loop

The most commonly used loop is the "for" loop. In shell scripting, there are two types: one that is similar to C's "for" loop, and an iterator (list processing) loop.
Syntax for the first type of "for" loop (again, this type is only available in modern shells):
for ((initialization; condition; increment/decrement))
do
...statements...
done

Example:
#!/bin/sh
for (( i=1; $i <= 10; i++ ))
do
echo $i
done

This will produce a list of numbers from 1 to 10. The syntax for the second, more widely-available, type of "for" loop is:
for <variable> in <list>
do
...statements...
done

This script will read the contents of '/etc/group' and display each line, one at a time:
#!/bin/sh
count=0
for i in `cat /etc/group`
do
count=`expr "$count" + 1`
echo "Line $count is being displayed"
echo $i
done
echo "End of file"

Another example of the "for" loop uses "seq" to generate a sequence:
#!/bin/sh
for i in `seq 1 5`
do
echo $i
done

While Loop

The "while" loop is another useful loop used in all programming languages; it will continue to execute until the condition specified becomes false.
while [ condition ]
do
...statement...
done

The following script assigns the value "1" to the variable num and adds one to the value of num each time it goes around the loop, as long as the value of num is less than 5.

#!/bin/sh
num=1
while [$num -lt 5]; do num=$[$num + 1]; echo $num; done

Select and Case Statement

Similar to the "switch/case" construct in C programming, the combination of "select" and "case" provides shell programmers with the same features. The "select" statement is not part of the "case" statement, but I've put the two of them together to illustrate how both can be used in programming.
Syntax of select:
select <variable> in <list>
do
...statements...
Done

Syntax of case:
case $<variable> in
<option1>) statements ;;
<option2>) statements ;;
*) echo "Sorry, wrong option" ;;
esac

The example below will explain the usage of select and case together, and display options involving a machine's services needing to be restarted. When the user selects a particular option, the script starts the corresponding service.

#!/bin/bash
echo "***********************"
select opt in apache named sendmail
do
case $opt in
apache) /etc/rc.d/init.d/httpd restart;;
named) /etc/rc.d/init.d/named restart;;
sendmail) /etc/rc.d/init.d/sendmail restart;;
*) echo "Nothing will be restarted"
esac
echo "***********************"

# If this break is not here, then we won't get a shell prompt.
break

done

[ Rather than using an explicit 'break' statement - which is not useful if you want to execute more than one of the presented options - it is much better to include 'Quit' as the last option in the select list, along with a matching case statement. -- Ben ]

Functions

In the modern world where all programmers use the OOP model for programming, even we shell programmers aren't far behind. We too can break our code into small chunks called functions, and call them by name in the main program. This approach helps in debugging, code re-usability, etc.
Syntax for "function" is:
<name of function> ()
{ # start of function
statements
} # end of function
Functions are invoked by citing their names in the main program, optionally followed by arguments. For example:
#!/bin/sh
sumcalc ()
{
sum=$[$1 + $2]
}

echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2

sumcalc $num1 $num2

echo "Output from function sumcalc: $sum"

Debugging Shell Scripts

Now and then, we need to debug our programs, we use the '-x' and '-v' options of the shell. The '-v' option produces verbose output. The '-x' option will expand each simple command, "for" command, "case" command, "select" command, or arithmetic "for" command, displaying the expanded value of PS4, followed by the command and its expanded arguments or associated word list. Try them in that order - they can be very helpful when you can't figure out the location of a problem in your script.
Shell Variable:

1. The PATH variable

One of the more important variables is the path variable. The path variable controls where the shell searches for commands, when you type them to at the prompt.

2. HOME
This is the home directory of your account. It is also used as the default when typing cd without any arguments.
3. PS1
The primary prompt string, normally set to `$ '. This is what the computer prints whenever it is ready to process another command.
4. PS2
The secondary prompt string, normally set to `> '. The shell prints this prompt whenever you have type an incomplete line, e.g., if you are missing a closing quote. For example:
$ echo 'hello
> world'
hello
world
5. SHELL
This variables give the path of the shell you are currently executing, e.g., if you are executing the standard Bourne shell, the SHELL variables is usually set to /bin/sh.
6. IFS
This is set to the internal field separators, normally set to space, tab, and blank. We will talk more about this later.

One-liners

Here are some one-liners that might come in handy some time. These one-liners serve both as educational examples that solves "real problems". If you have suggestions for more one-liners, please mail me.

Renaming several files at the same time

If you have a number of files named foo.C, bar.C.gz, etc. and want to rename them to foo.cc, bar.cc.gz, etc. This line will do the trick.
\ls *.C* | sed 's/\(.*\).C\(.*\)/mv & \1.cc\2/' | sh
The backslash before the ls command is to prevent is from being expanded, in the case that is is an alias and you are using shell that has aliases (such as Bash). We want to prevent the shell from doing this expansion since ls might come out as ls -F (which would behave strange) or ls -l which is really bad.
An alternative is to install the rename script, which is written in Perl.

Remove processes matching some regular expression

If you have a number of processes that you want to kill, one of the following one-liners might be useful:
kill `ps xww | grep "sleep" | cut -c1-5` 2>/dev/null
ps xww | grep "sleep" | cut -c1-5 | xargs kill 2>/dev/null
This will kill any processes that has the word "sleep" in the calling command. If your kill does not handle multiple pids' you can either use the one-liner
ps xww | grep "sleep" | cut -c1-5 | xargs -i kill {} 2>/dev/null
or use a for-loop:
for x in `ps xww | grep "sleep" | cut -c1-5`
do
kill $x 2>/dev/null
done
But then it is not a one-liner any more.
Note! Be very careful about what you use as expression to grep. You might get more processes than you wanted killed.