Saturday, March 30, 2019


Function: -
Q) Write a simple script to know function ?
Ans –
#! /bin/sh
fun()
{
echo “This is my first function”
}
fun

Q) Taking two variables from keyboard and find summation of it by function.
Ans –
#! /bin/sh
sum()
{
s=`expr $1 + $2`
echo “Result = $s”
}
echo “Enter two values: “
read a b
sum $a $b
Q) Find all arithmetic operation using function.
Ans –
#! /bin/sh
add()
{
s=`expr $1 + $2`
echo “Sum = $s”
}
diff()
{
d=`expr $1 - $2`
echo “Difference = $d”
}
mul()
{
m=`expr $1 \* $2`
echo “Multiplication = $m”
}
div()
{
di=`expr $1 / $2`
echo “Division = $di”
}
mod()
{
mo=`expr $1 % $2`
echo “Modulus = $mo”
}
echo “Enter two values: “
read a b
add $a $b
diff $a $b
mul $a $b
div $a $b
mod $a $b

Q) Write a script to know return of value ?
Ans –
#! /bin/sh
sum()
{
s=`expr $1 + $2`
return $s
}
echo “Enter two values: “
read a b
sum $a $b
echo “Result = $?”

Q) Write a script to add first two numbers and then subtract the third one ?
Ans –
#! /bin/sh
add()
{
s=`expr $1 + $2`
return $s
}
sub()
{
su=`expr $1 - $2`
return $su
}
echo “Enter three numbers: “
read a b c
add $a $b
r=”$?”
sub $r $c
echo “Result = $?”

Q) Write an example to know nested function ?
Ans –
#! /bin/sh
fun()
{
echo “This is my first function”
func
}
func()
{
echo “This is my second function”
fund
}
fund()
{
echo “This is my last function”
}
fun

Q) Take four values from user input and add first two ones, then subtract the third one from first addition result and then multiply fourth one from result of subtraction using nesting function ?
Ans –
#! /bin/sh
add()
{
s=`expr $1 + $2`
diff $s $3 $4
}
diff()
{
di=`expr $1 - $2`
mul $di $3
}
mul()
{
m=`expr $1 \* $2`
echo “Result = $m”
}
echo “Enter four values: “
read a b c d
add $a $b $c $d

For Loop: -
Q) Write a shell script to print 1 to 10 in for loop ?
Ans –
1st Way -
#! /bin/sh
for i in 1 2 3 4 5 6 7 8 9 10
do
echo “$i”
done
OR
2nd Way –
#! /bin/sh
for i in {1..10}
do
echo “$i”
done
OR
3rd Way –
#! /bin/sh
for ((i=1; i<=10; i++))
{
echo “$i”
}

Until Loop: -
Q) What is until loop ?
Ans –
1.       This loop is just opposite of while loop.
2.       The statement written in until block condition if false, then while block get executed otherwise not executed.

Q) Print 1 to 10 using until loop ?
Ans –
#! /bin/sh
i=1
until [ $i –gt 10 ]
do
echo “$i”
i=`expr $i + 1`
done

Break Statement: -
Q) What is break statement ?
Ans –
This keyword is used to terminate the loop and come out from the loop even though the condition is still satisfying.
Example – 1: -
#! /bin/sh
i=1
while [ $i –le 10 ]
do
echo “$i”
if [ $i –eq 5 ]
then
break
fi
i=`expr $i + 1`
done
Output – 1 2 3 4 5
Example – 2: -
#! /bin/sh
for ((i=1; i<=10;i++))
{
                     for ((j=1;j<=10;j++))
                     {
                     echo –n “$j”
                     if [ $j –eq 5 ]
                     then
                     break 2
                     fi
                     }
}
Output – 1 2 3 4 5

Continue Statement: -
Q) What is continue statement ?
Ans –
This keyword is used to skip the remaining statements and start the next iteration of the loop.
Example –
#! /bin/sh
i=0
while [ $i –le 10 ]
do
                     i=`expr $i + 1`
                     if [ $i –eq 5 ]
                     then
                     continue
                     fi
                     echo “$i”
done

Select Loop: -
Q) Write an example of select loop to understand it ?
Ans –
$ vi SelectExample.sh
#! /bin/sh
select drink in tea coffee juice water all none
do
                     case $drink in
                     tea | coffee | water)
                     echo “Go to caferteria”
                     ;;
                     Juice)
                     echo “Available at juice counter in cafeteria”
                     ;;
                     all)
                     echo “Available at home or outside”
                     ;;
                     None)
                     Break
                     ;;
                     *)
                     echo “Invalid Option”
                     ;;
                     esac
done
ESC + :wq<Enter – Key>
$ chmod 755 SelectExample.sh
$./SelectExample.sh
1.       Tea
2.       Coffee
3.       Water
4.       Juice
5.       All
6.       None
Enter Value: 1
Go to cafeteria
Enter Value: 8
Invalid Option
Enter Value: 6
$

Fibonacci Series: -
Q) Write a script to print Fibonacci series for first 20 elements.
0 1 1 2 3 5 8 13 …
Ans –
#! /bin/sh
i=0
j=1
count=3
echo “Fibonacci Series: $i $j “
while [ $count –le 20 ]
do
c=`expr $i + $j`
echo “$c”
i=$j
$j=$c
count=`expr $count + 1`
done

Command Substitution: -
Q) Write examples of command substitution ?
Ans –
Example – 1:
#! /bin/sh
x=`ls –lrt`
echo “Listing of current directories: “
echo $x
Example – 2:
#! /bin/sh
x=`date`
y=`who`
z=`ls`
m=`ps`
echo “Current date is $x”
echo “Logged in users are: “
echo $y
echo “listing will be: “
echo $z
echo “Processes will be: “
echo $m

Array: -
Q) Write a script to read all elements of array and print it ?
Ans –
#! /bin/sh
a=(1 ‘mm’ ’23-Apr-2019’)
len=${#a[@]} 
##length of array
for ((i=0;i<=len;i++))
{
echo “${a[i]}”
}
Q) Write a script to read input of array in run time and print it ?
Ans –
#! /bin/sh
echo “Enter the length of the array: “
read leng
echo “Enter Array Elements: “
for ((i=0;i<leng;i++))
{
echo “Enter array `expr $i + 1` element: “
read ${a[i]}
}
echo “The array elements are: “
for ((i=0;i<leng;i++))
{
echo “${a[i]}”
}

Assigning a string to array: -
Q) Assign the string 1234 5678 1223456 to an array and print it.
Ans –
#! /bin/sh
str=”1234 5678 1223456”
IFS=’ ‘read –a arr <<< ${str}
###IFS = Internal Field Separator
len=${#arr[@]}
###length of array
echo “Array Elements are: “
for ((i=0;i<=len;i++))
{
echo ${arr[i]}
}

Reading from a file:
Q) Take the filename from command prompt and read it line by line.
Ans –
$ vi script10.sh
#! /bin/sh
While read line
do
echo $line
done < $1
ESC + :wq<Enter – Key>
$ chmod 755 script10.sh
$ ./script10.sh test.dat<Enter – Key>

Q) Take the user input as file and show the lines of the file with line number will be the first character present for all lines.
Ans –
#! /bin/sh
echo “Enter the file name: “
read fn
i=1
while read line
do
echo “$i. $line”
i=`expr $i + 1`
done < $fn

Part – 2: -
Q) Output –
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
Write a script to get above output ?
Ans –
#! /bin/sh
i=1
while [ $i –le 4 ]
do
                     j=1
                     while [ $j –le 4 ]
                     do
                     echo –n “$j”
                     j=`expr $j + 1`
                     done
echo –e “\n”
i=`expr $i + 1`
done

Q) Output –
*
* *
* * *
* * * *
* * * * *
Ans –
#! /bin/sh
i=1
while [ $i –le 5 ]
do
                     j=1
while [ $j –le $i ]
                     do
                     echo –n “*”
                     j=`expr $j + 1`
                     done
echo “\n”
i=`expr $i + 1`
done

Q) Output –
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Ans –
#! /bin/sh
i=1
while [ $i –le 5 ]
do
                     j=1
                     while [ $j –le $i ]
                     do
                     echo –n “$j”
                     j=`expr $j + 1`
                     done
echo –e “\n”
i=`expr $i + 1`
done

Q) Output -
* * * * *
* * * *
* * *
* *
*
Ans –
#! /bin/sh
i=5
while [ $i –ge 1 ]
do
                     j=1
                     while [ $j –le $i ]
                     do
                     echo –n “*”
                     j=`expr $j + 1`
                     done
echo –e “\n”
i=`expr $i – 1`
done

Q) Output –
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Ans –
#! /bin/sh
i=5
while [ $i –le 1 ]
do
                     j=1
                     while [ $j –le $i ]
                     do
                     echo –n “$j”
                     j=`expr $j + 1`
                     done
echo –e “\n”
i=`expr $i – 1`
done

Q) Output –
1
2 3
4 5 6
7 8 9 10
Ans –
#! /bin/sh
i=1
k=0
while [ $i –le 4 ]
do
                     j=1
                     while [ $j –le $i ]
                     do
                     echo –n “`expr $j + $k`”
                     j=`expr $j + 1`
                     done
echo –e “\n”
k=`expr $k + 1`
i=`expr $i + 1`
done

Q) Output –
10
10 9
10 9 8
10 9 8 7
10 9 8 7 6
Ans –
#! /bin/sh
i=1
while [ $i –le 5 ]
do
                     j=1
                     k=10
                     while [ $j –le $i ]
                     do
                     echo –n “$k”
                     k=`expr $k – 1`
                     j=`expr $j + 1`
                     done
echo –e “\n”
i=`expr $i + 1`
done

Decision Making Statements: -

Q) What are decision making statements ?
Ans – There are two decision making statements. Such as –
1.       If else fi
2.       case esac

Q) Write a shell script whether a given number will be even or odd ?
Ans –
#! /bin/sh
echo “Enter a number: “
read n
if [ `expr $n % 2` -eq 0 ]
then
echo “$n is even number”
else
echo “$n is odd number”
fi

Q) Take two numbers from command line and check which is greater ?
Ans –
$ vi script7.sh
#! /bin/sh
If [ $1 –gt $2 ]
then
echo “$1 is greater than $2”
else
echo “$2 is greater than $1”
fi
ESC + :wq<Enter – Key>
$ chmod 755 script7.sh
$ ./script7.sh 23 45<Enter – Key>
Output –
45 is greater than 23

Q) Take two numbers as user input. Get the summation of it, if sum is greater than 10, then multiply it with 2 else multiply it with 3 and display the result ?
Ans –
#! /bin/sh
echo “Enter two numbers: “
read a b
s=`expr $a + $b`
if [ $s –gt 10 ]
then
echo “Result = `expr $s \* 2`”
else
echo “Result = `expr $s \* 3`”
fi

Q) Write a script to swap two numbers without help of third variable ?
Ans –
#! /bin/sh
echo “Enter two numbers: “
read a b
echo “Before swapping a = $a and b = $b”
a=`expr $a + $b`
b=`expr $a - $b`
a=`expr $a - $b`
echo “After swapping a = $a and b = $b”

Q) Write a script to swap two numbers with help of third variable ?
Ans - 
#! /bin/sh

echo “Enter two numbers: “
read a b
echo “Before swapping a = $a and b = $b”

c=$a
$a=$b
$b=$c
echo “After swapping a = $a and b = $b”

Q) What are syntax of different if else fi statements ?
Ans –
Simple IF:
If [ Condition ]
then
statements
fi
IF ELSE FI:
If [ Condition ]
then
statements
else
statements
fi
Nested IF:
If [ Condition ]
then
statements
                     If [ Condition ]
                     then
                     statements
                     else
                     statements
                     fi
else
statements
                     If [ Condition ]
                     then
                     statements
                     else
                     statements
                     fi
fi
ELSE IF LADDER:
If [ Condition ]
then
statements
elif [ Condition ]
then
statements
elif [ Condition ]
then
statements
else
statements
fi

Q) What is the syntax of case esac statements ?
Ans –
case variable in
Value1)
echo “Statements”
;;
Value2)
echo “statements”
;;
0)
exit
;;
esac

Q) Write a script to know case esac statements ?
Ans –
#! /bin/sh
echo “Enter the state name or zero to exit: “
read state
case $state in
“Odisha”)
echo “Bhubaneswar”
;;
“Karnataka”)
echo “Bengaluru”
;;
0)
exit
;;
esac

Q) What are shell loops ?
Ans –
1.       while
2.       until
3.       for
4.       select

Q) What is the syntax of while loops ?
Ans –
while [ Condition ]
do
statements
done


Part – 1: -
Q) Write a script to print 1 to 10 ?
Ans –
#! /bin/sh
i=1
while [ $i –le 10 ]
do
echo “$i”
i=`expr $i + 1`
done

Q) Write a script to print all even numbers between 50 to 100 ?
Ans –
#! /bin/sh
i=50
while [ $i –le 100 ]
do
echo “$i”
i=`expr $i + 2`
done

Q) Print first 10 even numbers ?
Ans –
#! /bin/sh
i=1
count = 1
while [ $count –le 10 ]
do
echo “`expr $i \* 2`”
count=`expr $count + 1`
i=`expr $i + 1`
done

Q) Write a script to know whether a number is prime or not ?
Ans –
echo “Enter a number: “
read num
i=1
count=0
while [ $i –le $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
count=`expr $count + 1`
fi
i=`expr $i + 1`
done
if [ $count –eq 2 ]
then
echo “$num is a prime number”
else
echo “$num is not a prime number”
fi

Q) Check whether a number is palindrome or not ?
Ans –
#! /bin/sh
echo “Enter a number: “
read num
i=0
j=$num
while [ $j –gt 0 ]
do
rem=`expr $j % 10`
i=`expr $i \* 10 + $rem`
j=`expr $j / 10`
done
if [ $i –eq $num ]
then
echo “$num is a palindrome number”
else
echo “$num is not a palindrome number”
fi

Friday, March 29, 2019


Part – 2:
Q) How we get the PID of current shell ?
Ans - $ echo “$$”<Enter – Key>
Q) How we get the exit status of last command ?
Ans - $ echo “$?”<Enter – Key>
Q) How we get the process number of last back ground process ?
Ans - $ echo “$!”<Enter – Key>
Q) What are different types of Unix Special Variable ?
Ans –
$0 – Return Current Script File Name
$1 – Return 1st Command line argument
$2 – Return 2nd Command line argument
$n – Return nth Command line argument
$# - Return total number of Command line arguments
$@ or $* - Return all command line arguments within double quotes.
Example –
vi script6.sh
#! /bin/sh
echo “The PID of current Shell: $$”
echo “The total number of command line arguments: $#”
echo “The 1st command line argument: $1“
echo “The 2nd command line argument: $2”
echo “The name of your script: $0”
ESC + :wq<Enter – Key>
$ sh script6.sh 23 12<Enter – Key>
Output –
The PID of current shell: 2354
The total number of command line arguments: 2
The 1st command line argument: 23
The 2nd command line argument: 12
The name of your script: script6.sh
Q) What are the operators in Shell Script ?
Ans – There are 5 operators present in shell script. Such as –
1. Arithmetic Operator
2. Relational Operator
3. Boolean Operator
4. String Operator
5. File Test Operator
Q) What are arithmetic operators ?
Ans –
+ -> `expr $a + $b` Addition Operator
- -> `expr $a - $b` Subtraction Operator
* -> `expr $a \* $b` Multiplication Operator
/ -> `expr $a / $b` Division Operator
% -> `expr $a % $b` Modulus Operator
= -> c=`expr $a + $b` Assignment Operator
== -> if [ $a == $b ] Comparison Operator – if both string or character are equal return true otherwise false
!= -> if [ $a != $b ] Comparison Operator - if both string or character are not equal return true otherwise false
Where a and b are operands.
Example –
#! /bin/sh
a=45
b=23
s=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
d=`expr $a / $b`
mod=`expr $a % $b`
echo “Sum = $s”
echo “Subtract = $sub”
echo “Multiplication = $mul”
echo “Division = $d”
echo “Modulus = $mod”
Q) What are Relational operators ?
Ans –
For numbers, it operates.
-eq -> equal operator
-ne -> Not equal operator
-gt -> greater than operator
-ge -> greater than equal to
-lt -> less than
-le -> less than equal to
Example 
a=34
b=21
if [ $a –eq $b ]
Q) What are Boolean operators ?
Ans –
-o (logical OR) – It takes multiple conditions, any one condition is true, then total expression is true.
-a (logical AND) – It takes multiple conditions and returns true if all the conditions are true.
Q) What are String Operators ?
Ans –
= -> check if the value of two string operands are equal or not
!= -> check if the value of string operands are not equal if yes return true else false
-z -> Check if the size of operand is zero if yes return true else false.
-n -> check if the size of operand is non – zero if yes returns true else false.
Q) What are File Test Operators ?
Ans –
If [ -r $FileName ] -> checks if the file is readable or not
If [ -w $FileName ] -> checks if the file is writable or not
If [ -x $FileName ] -> checks if the file is executable or not
If [ -f $FileName ] -> checks if the file is ordinary file or not
If [ -d $FileName ] -> checks if the file is directory file or not
If [ -e $FileName ] -> checks if the file name any other files or directories exists or not
If [ -s $FileName ] -> checks if the size of file is non – zero or not

Thursday, March 28, 2019


Shell Scripting: -
Part – 1:
Q) Why we go for shell scripting ?
Ans – In Unix command prompt, we can run a single command at a time. To combine and run multiple command for performing certain tasks, shell script is evolved.
Q) What is shebang or Hash line or magic line or dang line ?
Ans – The 1st line of the shell script is called hash line or shebang or magic line or dang line.
It is written as below.
For default shell,
#! /bin/sh
For K – Shell,
#! /bin/ksh
Q) Which command is used in shell script to print output in screen ?
Ans – echo command
Q) Print ‘Hello World’ in shell script ?
Ans –
#! /bin/sh
echo “Hello World”
Q) How to run a shell script ?
Ans – There are two ways to run a shell script. Such as –
1. 1st Way –
$ vi script1.sh<Enter – Key>
#! /bin/sh
echo “Hello World”
Esc + :wq<Enter – Key>
$ chmod 755 script1.sh<Enter – Key>
$ ./script1.sh<Enter – Key>
Hello World
2. 2nd Way –
#! /bin/sh
echo “Hello World”
Esc + :wq<Enter – Key>
$ sh script1.sh<Enter – Key>
For K – shell, $ ksh script1.ksh<Enter – Key>
Q) How to comment lines present in shell script ?
Ans – By inserting single ‘#’ or multiple ‘#’ in the beginning of the line make the line comment.
Example –
$ vi script2.ksh
#! /bin/ksh
###echo “Hello World” – Commented line
echo “Time Begins” – Uncommented line
###echo “Social Activity” – Commented line
ESC + :wq<Enter – Key>
$ ksh script2.ksh<Enter – Key>
Output – Time Begins
Q) What are variables in shell script ?
Ans – There are two types of shell script variable. Such as –
1. Scalar Variable – This Variable holds data of any data type. It stores only a single value at a time.
Example –
$ vi script3.ksh
#! /bin/ksh
i=5
echo “i value = $i”
i=4.5
echo “i value = $i”
i=’India’
echo “I value = $i”
i=’you are a good boy’
echo “i value = $i”
Esc + :wq<Enter – Key>
$ ksh script3.ksh<Enter – Key>
Output –
i value = 5
i value = 4.5
i value = India
i value = you are a good boy
2. Array – To store multiple values of different datatype in continuous memory location, we go for array.
Q) What are ways to read input from command line ?
Ans – There are two ways to read an input from command line. Such as –
1. 1st Way –
$ vi script4.sh
#! /bin/sh
echo “Enter values of a and b: “
read a b
echo “Value of a = $a and Value of b = $b”
Esc + :wq<Enter – Key>
$ sh script4.sh<Enter – Key>
Output –
Enter values of a and b:
23 13
Value of a = 23 and Value of b = 13
2. 2nd Way –
$ vi script5.sh
#! /bin/sh
echo “Value of a = $1”
echo “Value of b = $2”
echo “Value of c = $3”
Esc + :wq<Enter – Key>
$ sh script5.sh 43 23 67<Enter – Key>
Output –
Value of a = 43
Value of b = 23
Value of c = 67
Or
$ chmod 755 script5.sh<Enter – Key>
$./script5.sh 43 23 67<Enter – Key>
Output –
Value of a = 43
Value of b = 23
Value of c = 67
Q) What is the output of below script ?
#! /bin/sh
i=5
j=7
echo “Value of I = $i and j = $j”
echo ‘Value of I = $i and j = $j”
Ans –
Value of I = 5 and j = 7
Value of I = $i and j = $j
Q) Which variable is used in shell script to keep the value of the variable constant throughout the script?
Ans – readonly variable
Example –
#! /bin/sh
a=78
echo “Value of a = $a”
readonly a
a=56
echo “Value of a = $a”
Output –
Value of a = 78
Value of a = 78

Interview Questions: - (UNIX - 0) UNIX: -   Q) How to get inode number of a file/directory? Ans – $ ls –i <File/Directory_Name...