You are on page 1of 20

A

Lab Manual Report

On

“Linux shell scripting Lab”


Submitted in partial fulfilment for the

Award of the degree of

BACHELOR OF TECHNOLOGY

IN
Department of Computer Engineering

Engineering College Bikaner (Bikaner)

During 2019-2020

Submitted To : Submitted By:


Mrs. Charu Jain
Ishan Ahmed

Asst. Prof of CSE. Dept Roll No : 18EEBCS033

BTech 4th Sem


List of Experiments:
1. Use of Basic Unix Shell Commands: ls, mkdir, rmdir, cd, cat, banner,
touch, file, wc, sort, cut, grep, dd, dfspace, du, ulimit.

2. Commands related to inode, I/O redirection and piping, process control


commands, mails.

3. Shell Programming: Shell script based on control structure- If-then-fi, if-then-


else-if, nested if-else, to find:

3.1 Greatest among three numbers.


3.2 To find a year is leap year or not.
3.3 To input angles of a triangle and find out whether it is valid triangle or not.

3.4 To check whether a character is alphabet, digit or special character.


3.5 To calculate profit or loss.

4. Shell Programming - Looping- while, until, for loops

4.1 Write a shell script to print all even and odd number from 1 to 10.
4.2 Write a shell script to print table of a given number
4.3 Write a shell script to calculate factorial of a given number.
4.4 Write a shell script to print sum of all even numbers from 1 to 10.

4.5 Write a shell script to print sum of digit of any number.


5. Shell Programming - case structure, use of break

5.1 Write a shell script to make a basic calculator which performs addition
subtraction, Multiplication, division
5.2 Write a shell script to print days of a week.

5.3 Write a shell script to print starting 4 months having 31 days.


6. Shell Programming - Functions

6.1 Write a shell script to find a number is Armstrong or not.


6.2 Write a shell script to find a number is palindrome or not.

6.3 Write a shell script to print Fibonacci series.


6.4 Write a shell script to find prime number.
6.5 Write a shell script to convert binary to decimal and decimal to binary

7. Write a shell script to print different shapes- Diamond, triangle, square,


rectangle, hollow square etc.
8. Shell Programming – Arrays

8.1 Write a C program to read and print elements of array.


8.2 Write a C program to find sum of all array elements.
8.3 Write a C program to find reverse of an array.

8.4 Write a C program to search an element in an array.

8.5 Write a C program to sort array elements in ascending or descending


order.
Experiment 1…

1. Use of Basic Unix Shell Commands: ls, mkdir, rmdir, cd, cat, banner,
touch, file, wc, sort, cut, grep, dd, dfspace, du, ulimit.
Experiment no. 3
3. Shell Programming: Shell script based on control structure- If-then-fi, if-then-
else-if, nested if-else, to find:

3.1 Greatest among three numbers

echo "Enter Num1"


read num1
echo "Enter Num2"
read num2
echo "Enter Num3"
read num3

if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]


then
echo $num1
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo $num2
else
echo $num3
fi

3.2 To find a year is leap year or not.

echo "Enter Year:"


read y
 
year=$y
 
y=$(( $y % 4 ))
if [ $y -eq 0 ]
then
    echo "$year is Leap Year!"
else
    echo "$year is not a Leap Year!"
fi

3.3 To input angles of a triangle and find out whether it is valid triangle or not.
echo"enterangleA"
readA
echo"enterangleB"
readB
echo"enterangleC"
readC
#sumallthreeangles
d=$((A+B+C))
if[$A-eq0-o$B-eq0-$C-eq0]
then
echo"Enteranglesgreaterthazero"
else
   if[$d==180];
   then
   echo"validtraingle"
   else
   echo"notavalidtraingle"
   fi
fi

3.4 To check whether a character is alphabet, digit or special character

echo "enter a char"


read c

if [[ $c == [A-Z] ]];
then
echo "upper"
elif [[ $c == [a-z] ]];
then
echo "lower"
else
echo "Digit or special symbols!"
fi

3.5 To calculate profit or loss

echo "input nthe cost price of an item"


read cop
echo "input the selling price of the item"
read sop
if test $cop -eq $sop
then
echo "no profit or no gain"
fi
if test $cop -gt $sop
then
s=`echo $cop - $sop |bc`
echo " u obtained loss of rs:$s"
else
s=`echo $sop - $cop |bc`
echo "u obtained profit of rs:$s"
fi

Experiment no. 4

4.Shell Programming - Looping- while, until, for loops

4.1 Write a shell script to print all even and odd number from 1 to 10.
echo “First Odd and Even number till 10 are”
n=1
while [ $n -lt 10 ]; do
   out=$(( $n % 2 ))
   if [ “$out” -eq 0 ] then
      echo “$n is even number” 
   else
      echo “$n is ODD number”
   fi
   n=$(( $n + 1 ))
                   done

4.2 Write a shell script to print table of a given number


echo "Enter a Number"
read n
i=0
while [ $i -le 10 ]
do
    echo " $n x $i = `expr $n \* $i`"
    i=`expr $i + 1`
done

4.3 Write a shell script to calculate factorial of a given number.

echo "Enter a number"


read num

fact=1

while [ $num -gt 1 ]


do
fact=$((fact * num)) #fact = fact * num
num=$((num - 1)) #num = num - 1
done

echo $fact

4.4 Write a shell script to print sum of all even numbers from 1 to 10.

echo “Enter upper limit”

read n

$i=2

while [$i -lt $n]

do

expr ‘$sum=$sum+$i’

expr ‘$i=$i+2’

done

echo “Sum is : $sum”

4.5 Write a shell script to print sum of digit of any number.

echo "Enter a number"


read num

sum=0

while [ $num -gt 0 ]


do
mod=$((num % 10)) #It will split each digits
sum=$((sum + mod)) #Add each digit to sum
num=$((num / 10)) #divide num by 10.
done

echo $sum
Experiment no. 5
5. Shell Programming - case structure, use of break

5.1 Write a shell script to make a basic calculator which performs addition subtraction,
Multiplication, division
# !/bin/bash
  
# Take user Input
echo "Enter Two numbers : "
read a
read b
  
# Input type of operation
echo "Enter Choice :"
echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"
read ch
  
# Switch Case to perform
# calulator operations
case $ch in
  1)res=`echo $a + $b | bc`
  ;;
  2)res=`echo $a - $b | bc`
  ;;
  3)res=`echo $a \* $b | bc`
  ;;
  4)res=`echo "scale=2; $a / $b" | bc`
  ;;
esac
echo "Result : $res"

5.2 Write a shell script to print days of a week.

for i in mon tue wed thurs fri sat


do
echo weekday: $i
if [ $i == thurs ]
then
echo weekend: $i+1
fi
done

5.3 Write a shell script to print starting 4 months having 31 days.

for m in {1..12}; do
echo $(date -d $m/1/1 +%b) - $(date -d "$(($m%12+1))/1 - 1 days" +%d) days
done

Experiment no. 6
6. Shell Programming - Functions

6.1 Write a shell script to find a number is Armstrong or not.

echo"Enter a number: "


read c

x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done

if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
fi

6.2 Write a shell script to find a number is palindrome or not.

#!/bin/bash

# GNU bash Script

n=12321

rev=$(echo $n | rev)
if [ $n -eq $rev ]; then

   echo "Number is palindrome"

else

   echo "Number is not palindrome"

fi

6.3 Write a shell script to print Fibonacci series.

Clear

echo "Program to Find Fibonacci Series"

echo "How many number of terms to be generated ?"

read n

x=0

y=1

i=2

echo "Fibonacci Series up to $n terms :"

echo "$x"

echo "$y"

while [ $i -lt $n ]

do

i=`expr $i + 1 `

z=`expr $x + $y `

echo "$z"

x=$y

y=$z

done

6.4 Write a shell script to find prime number.


number=53

i=2

flag=0

while test $i -le `expr $number / 2`

do

if test `expr $number % $i` -eq 0

then

flag=1

fi

i=`expr $i + 1`

done if test $flag -eq 1

then

echo "The number is Not Prime"

else

echo "The number is Prime"

Fi

6.5 Write a shell script to convert binary to decimal and decimal to binary

tput clear
echo "Conversion of decimal to Binary and Binary to Decimal"
echo "1. Convert Decimal to Binary"
echo "2. Convert Binary to Decimal"
echo "3. Exit"
echo "Enter ur choice:"
read ch
case $ch in
1) echo "Enter any decimal no:"
read num
rem=1
bno=" "
while [ $num -gt 0 ]
do
rem=`expr $num % 2 `
bno=$bno$rem
num=`expr $num / 2 `
done
i=${#bno}
final=" "
while [ $i -gt 0 ]
do
rev=`echo $bno | awk '{ printf substr( $0,'$i',1 ) }'`
final=$final$rev
i=$(( $i - 1 ))
done
echo "Equivalent Binary no:" $final ;;
2) echo "Enter any Binary no;"
read bino
len=${#bino}
i=1
pow=$((len - 1 ))
while [ $i -le $len ]
do
n=`echo $bino | awk '{ printf substr( $0,'$i',1 )}' `
j=1
p=1
while [ $j -le $pow ]
do
p=$(( p * 2 ))
j=$(( j + 1 ))
done
dec=$(( n * p ))
findec=$(( findec + dec ))
pow=$((pow - 1 ))
i=$(( i + 1 ))
done
echo "Equivalent Decimal no:"$findec ;;
3) echo "Enter correctly:" ;;
esac

Experiment no. 7

7. Write a shell script to print different shapes- Diamond, triangle, square, rectangle,
hollow square etc.

For diamond
#!/bin/bash

MAX=0
echo -n “Enter Number between (5 to 9) : ”
read MAX

if ! [ $MAX -ge 5 -a $MAX -le 9 ]


then
echo “Pls enter number between 5 and 9, Try Again”
exit 1
fi
clear

for (( i=1; i<=MAX; i++ ))


do
for (( s=MAX; s>=i; s– ))
do
echo -n ” ”
done
for (( j=1; j<=i;  j++ ))
do
echo -n ” $”
done
echo “”
done
###### Second stage ######################
for (( i=MAX; i>=1; i– ))
do
for (( s=i; s<=MAX; s++ ))
do
echo -n ” ”
done
for (( j=1; j<=i;  j++ ))
do
echo -n ” $”
done
echo “”
done

For Triangle

rows=5
for((i=1; i<=rows; i++))
do
  for((j=1; j<=rows - i; j++))
  do
    echo -n "  "
  done
  for((j=1; j<=2*i - 1; j++))
  do
    echo -n "* "
  done
  echo
done

For rectangle and square

echo "Size of the square?"


read size

clear

for (( i=0;i<size;i++ ))
do
echo "*"
done
For Hollow square

rows=3
cols=5
for ((i=1; i<=rows; i++))
do
for ((j=1; j<=cols; j++))
do
if ((i==1 || i==rows) || (j==1 || j==cols))
then
echo "*"
else
echo " ";
fi
done
echo;
done

Experiment no. 8
8. Shell Programming – Arrays

8.1 Write a C program to read and print elements of array.

int main()
{
    int a[1000],i,n;  

     printf("Enter size of array: ");


    scanf("%d",&n);
 
     printf("Enter %d elements in the array : ", n);
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
 
    printf("\nElements in array are: ");
    for(i=0;i<n;i++)
 
    {
        printf("%d  ", a[i]);
    }
 
    return 0;
}
8.2 Write a C program to find sum of all array elements.

#include <conio.h>
 
int main()
{
    int a[1000],i,n,sum=0;
  
    printf("Enter size of the array : ");
    scanf("%d",&n);

    printf("Enter elements in array : ");


    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }

    
    for(i=0; i<n; i++)
    {
        
        sum+=a[i];
    }
     printf("sum of array is : %d",sum);
 
    return 0;
}

8.3 Write a C program to find reverse of an array.


#include<stdio.h>

int main()
{
int a[100], i, j, Size, Temp;

printf("\nPlease Enter the size of an array: ");


scanf("%d",&Size);

for (i = 0; i < Size; i++)


{
scanf("%d", &a[i]);
}

j = i - 1; // Assigning j to Last array element


i = 0; // Assigning i to first array element

while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}

printf("\nResult of an Reverse array is: ");


for (i = 0; i < Size; i++)
{
printf("%d \t", a[i]);
}

return 0;
}

8.4 Write a C program to search an element in an array.

#include <stdio.h>

#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

printf("\nEnter element to search: ");


scanf("%d", &toSearch);

found = 0;

for(i=0; i<size; i++)


{
if(arr[i] == toSearch)
{
found = 1;
break;
}
}

if(found == 1)
{
printf("\n%d is found at position %d", toSearch, i + 1);
}
else
{
printf("\n%d is not found in the array", toSearch);
}

return 0;
}

8.5 Write a C program to sort array elements in ascending or descending order.


#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;

printf("Enter size of array: ");


scanf("%d", &size);

printf("Enter elements in array: ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

for(i=0; i<size; i++)


{

for(j=i+1; j<size; j++)


{
if(arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nElements of array in ascending order: ");
for(i=0; i<size; i++)
{
printf("%d\t", arr[i]);
}

return 0;
}

You might also like