Interview Questions

Scripting

Appending files in Unix

Two or more files can be appended in Linux using 'cat' command.

cat fileName1,fileName2 > fileName3

The above command will append the contents of fileName2 in fileName1 and the resulting data will be available in fileName3.

Consider there are two files emp.txt(contains empId:empName) and designation.txt(contains empId:designation).

emp.txt

100:CHRIS
101:MARTIN

designation.txt

100:HR
101:SYSTEM_ANALYST

JavaScript Animation

• Animation in JavaScript can be done by changing a JavaScript on different events for different images.
• This is done with the help of DOM elements. We can move these DOM elements with respect to some specified pattern.
• Also to make any object animate we would need a time frame so that the objects defined can animate/move accordingly.
• Let’s see an example on this.

Illustration :

<html>
<head>
<title>Demo on Animation with Java script</title>

<style type="text/css">
#Objcar {
position:absolute;
left:0px;
top:7em;
width:6em;

Attachments: 

Shell Script – IF Loop

The IF condition is used to take decision in scripting i.e., used to execute blocks of code based on the given condition which evaluates to true or false.

Syntax

If condition
then
        //command X executes when condition is true
else
        //command Y executes when condition is false
fi

If loop – Using Integer comparison

#!/bin/sh
x=1

if [ $x -eq 1 ]
then
        echo "value of x is 1"
fi

If - else - if loop – Using String comparison

 #!/bin/sh
name=test

if [ $name == "test" ]

Shell Script - Find Unique Elements in a File

In this article, let's create a shell script to find unique elements in a file containing a list of Strings.

Consider the below file containing a list of Strings which are repeated.

Data.txt

Afghanistan
India
Albania
Algeria
Austria
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Afghanistan
Dubai
Indonesia
Armenia

Look at the shell script below.

Unique.sh

#!/bin/sh
sort Sata.txt > SortData.txt
prev=0;
for f in $(cat SortData.txt | sed 's/ //g')
do
if [ $f == $prev ]; then

Shell Script – Read User Input

In this article, let us see how to get input from user in a Shell script using read command.

Command Usage :

read <options> variable_name_1 variable_name_2 variable_name_N<variable>

Options are,
-d delimiter -> the first delimiter character is used to stop the user input rather than new line character.
-n number_of_chars -> this ‘n’ decides the number of characters that the user can input

Consider the below sample shell script which demonstrates how to get user input.

Sample script :
 

GREP and SED Command in Unix

In this article let's see the basic usage of GREP and SED commands in Unix platform.

What is Grep?

Grep (Global Regular Expression Processor) is a command used for searching for text, patterns (regex) in a file or a set of files. Let see the usage of Grep command using the following example.

country.txt

Afghanistan
Albania
Algeria
United States
Austria
Australia
Chile
Mongolia
China
France
Italy
India
Finland
Dubai
Indonesia
Armenia

To find the list of countries starting with ‘a’ use the following command

Shell Script – Arithmetic Operations

In this article let us see how to perform Shell script arithmetic operations.

In the Shell command line, in order to perform the arithmetic operation, we need to execute the below command,
$ expr 1 + 4

The same arithmetic operation can be performed in the Shell script as follows,

ArithmeticOperation.sh

echo "Arithmetic operations"
x=2
y=1
sum=`expr ${x} + ${y}`
diff=`expr ${x} - ${y}`
multiply=`expr ${x} \* ${y}` # For multiplication, use \* since * is a wild card character
division=`expr ${x} / ${y}`
modulus=`expr ${x} % ${y}`

Shell Script - Passing Command line arguments

In this article, let us see how to get the command line arguments in the Shell Script program.

Consider the following Shell Script which does addition of two numbers using the constant values.

echo "Program to add two numbers"
a=1
b=2
sum=`expr $a + $b`
echo "Sum = ${sum}"

Now, let’s see how to get these two values from command line arguments and perform addition.

Consider the following Shell Script which gets the values from the command line arguments.

echo "Program to get the command line argument"

Shell Script Tutorial

What is Shell?
In the Unix Operating System, Shell is the Command interpreter where you can execute System commands defined by the Operating System.

What is Shell Script?
Shell Script is a plain text file which has set of commands to be executed automatically. Generally, these commands are executed by the System users. Instead of executing all the commands manually, when these commands are placed in a common file for execution then it is termed as Shell script.

Why to use Shell Script?
1. Manual or repeated tasks can be automated easily.

Shell Script - Handling Signals in Shell Script

In this section, let us see how to handle interrupt signal in Shell script.

In a Linux machine, any running application can be interrupted or stopped by interrupting with signals.

For example, assume a Java process is getting executed which has process id "1234".
This Java process can be interrupted by sending SIGKILL(9) signal using the below command, kill -9 1234