"Linux Shell Scripting Lab": Bachelor of Technology IN
"Linux Shell Scripting Lab": Bachelor of Technology IN
On
BACHELOR OF TECHNOLOGY
IN
Department of Computer Engineering
During 2019-2020
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.
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.
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.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
if [[ $c == [A-Z] ]];
then
echo "upper"
elif [[ $c == [a-z] ]];
then
echo "lower"
else
echo "Digit or special symbols!"
fi
Experiment no. 4
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
fact=1
echo $fact
4.4 Write a shell script to print sum of all even numbers from 1 to 10.
read n
$i=2
do
expr ‘$sum=$sum+$i’
expr ‘$i=$i+2’
done
sum=0
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"
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
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
#!/bin/bash
n=12321
rev=$(echo $n | rev)
if [ $n -eq $rev ]; then
else
fi
Clear
read n
x=0
y=1
i=2
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
i=2
flag=0
do
then
flag=1
fi
i=`expr $i + 1`
then
else
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
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
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
int main()
{
int a[1000],i,n;
#include <conio.h>
int main()
{
int a[1000],i,n,sum=0;
printf("Enter size of the array : ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
sum+=a[i];
}
printf("sum of array is : %d",sum);
return 0;
}
int main()
{
int a[100], i, j, Size, Temp;
while (i < j)
{
Temp = a[i];
a[i] = a[j];
a[j] = Temp;
i++;
j--;
}
return 0;
}
#include <stdio.h>
int main()
{
int arr[MAX_SIZE];
int size, i, toSearch, found;
found = 0;
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;
}
int main()
{
int arr[MAX_SIZE];
int size;
int i, j, temp;
return 0;
}