Sunteți pe pagina 1din 7

CSE AND I.T.

3rd semester Scripting lab

1.Shell Script to make a menu driven calculator using case

2.Shell script to measure size of a file

3.Sort file abc.txt and save this sorted file in xyz.txt

4.Search a file for a pattern - grep

5.What is shell? List activity of the shell and types of shell

6.How would you know machine’s name in the network?

Filters in unix

7.Script to find the value of one number raised to the power of another

8.Write a shell program to concatinate two strings and find the length of the
resultent string

9.Update access, modification, and / or change times of file - touch

10.Calculate the gross salary

11.Shell script to check whether inputted directory is in current directory or not

12.Write a shell program to count the characters, count the lines and the words in a
particular file

13.How would u get online help of cat command?

14.How would u display the hidden files?

15.Shell Script for generating a mark sheet

16.Write a shell script to find sum of digits of a number

17.Write a shell program to convert all the contents into the uppercase in a
particular file
Code for Shell Script to make a menu driven calculator using case in Unix /
Linux

clear
sum=0
i="y"

echo " Enter one no."


read n1
echo "Enter second no."
read n2
while [ $i = "y" ]
do
echo "1.Addition"
echo "2.Subtraction"
echo "3.Multiplication"
echo "4.Division"
echo "Enter your choice"
read ch
case $ch in
1)sum=`expr $n1 + $n2`
echo "Sum ="$sum;;
2)sum=`expr $n1 - $n2`
echo "Sub = "$sum;;
3)sum=`expr $n1 \* $n2`
echo "Mul = "$sum;;
4)sum=`expr $n1 / $n2`
echo "Div = "$sum;;
*)echo "Invalid choice";;
esac
echo "Do u want to continue ?"
read i
if [ $i != "y" ]
then
exit
fi
done

Code for Write a shell program to concatinate two strings and find the length
of the resultent string in Unix / Linux
echo Enter first string:
read s1
echo Enter second string:
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo Concatinated stringis $s3 of length $len

Code for Write a shell script to find sum of digits of a number in Unix / Linux
echo -n "Input no : "
read no
length=`expr length $no`

while [ $length -ne 0 ]


do
b=`expr substr $no $length 1`
ans=`expr $ans + $b`
length=`expr $length - 1`
done
echo "Sum of Digit is : $ans"

Code for Write a shell script to find the largest among the 3 given numbers in
Unix / Linux
echo Enter 3 numbers with spaces in between
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
fi
if [ $c -gt $l ]
then
l=$c
fi
echo Lagest of $a $b $c is $l

Code for Write a shell script to find the smallest of three numbers in Unix /
Linux
echo Enter 3 numbers with spaces in between
read a b c
s=$a
if [ $b -lt $s ]
then
s=$b
fi
if [ $c -lt $s ]
then
s=$c
fi
echo Smallest of $a $b $c is $s

Code for Write a shell script to reverse a number supplied by a user in Unix /
Linux
if [ $# -eq 1 ]
then
if [ $1 -gt 0 ]
then
num=$1
sumi=0
while [ $num -ne 0 ]
do
lnum=`expr $num % 10`
sumi=`expr $sumi * 10 + $lnum`
num=`expr $num \/ 10`
done
echo "Reverse of digits is $sumi of $1"else
echo " Number is less than 0"
fi
else
echo "Insert only one parameter "
fi

------------------------------------------------------------------------------
--

output:

$ sh1 23456
Reverse of digits is 65432 of 23456

Code for Write a shell script to sort the given numbers in descending order
using Bubble sort in Unix / Linux
echo
i=1
k=1
echo "Enter no. of integers to be sorted"
read n
echo
echo "Enter the numbers"while [ $i -le $n ]
do
read num
x[$k]=`expr $num`
i=`expr $i + 1`
k=`expr $k + 1`
done
x[$k]=0
k=1
echo
echo "The number you have entered are"while [ ${x[$k]} -ne 0 ]
do
echo "${x[$k]}"
echo
k=`expr $k + 1`
done
k=1
while [ $k -le $n ]
do
j=1
while [ $j -lt $n ]
do
y=`expr $j + 1`
if [ ${x[$j]} -gt ${x[$y]} ]
then
temp=`expr ${x[$j]}`
x[$j]=`expr ${x[$y]}`
x[$y]=`expr $temp`
fi
j=`expr $j + 1`
done
k=`expr $k + 1`
done
k=1
echo
echo "Number in sorted order..."
echo
while [ ${x[$k]} -ne 0 ]
do
echo "${x[$k]}"
echo
k=`expr $k + 1`
done

Code for Write a shell program to find the gcd for the 2 given numbers in
Unix / Linux
echo Enter two numbers with space in between
read a b
m=$a
if [ $b -lt $m ]
then
m=$b
fi
while [ $m -ne 0 ]
do
x=`expr $a % $m`
y=`expr $b % $m`
if [ $x -eq 0 -a $y -eq 0 ]
then
echo gcd of $a and $b is $m
break
fi
m=`expr $m - 1`
done

Code for Write a shell script to find the sum, the average and the product of the
four integers entered in Unix / Linux
echo Enter four integers with space between
read a b c d
sum=`expr $a + $b + $c + $d`
avg=`expr $sum / 4`
dec=`expr $sum % 4`
dec=`expr \( $dec \* 1000 \) / 4`
product=`expr $a \* $b \* $c \* $d`
echo Sum=$sum
echo Average=$avg.$dec
echo Product=$product
Code for Prepare the marksheet of the student in Unix / Linux
echo "Enter student name"
read name
echo "Enter semester"
read sem

echo "Enter subject 1 name "


read sub1
echo "Enter mark"
read mark1
echo "Enter subject 2 name "
read sub2
echo "Enter mark"
read mark2
echo "Enter subject 3 name "
read sub3
echo "Enter mark"
read mark3
echo "Enter subject 4 name "
read sub4
echo "Enter mark"
read mark4
echo "Enter subject 5 name "
read sub5
echo "Enter mark"
read mark5
echo "Enter subject 6 name "
read sub6
echo "Enter mark"
read mark6

echo "Name : $name"


echo "semester : $sem"
echo "Subject Marks"
echo "$sub1 $mark1"
echo "$sub2 $mark2"
echo "$sub3 $mark3"
echo "$sub4 $mark4"
echo "$sub5 $mark5"
echo "$sub6 $mark6"
echo "Total Marks : `expr + $mark1 + $mark2 + $mark3 + $mark4 + $mark5 +
$mark6`"

Code for Write a shell program to count number of words, characters, white
spaces and special symbols in a given text in Unix / Linux
echo Enter a text
read text
w=`echo $text | wc -w`
w=`expr $w`
c=`echo $text | wc -c`
c=`expr $c - 1`
s=0
alpha=0
j=` `
n=1
while [ $n -le $c ]
do
ch=`echo $text | cut -c $n`
if test $ch = $j
then
s=`expr $s + 1`
fi
case $ch in
a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z) alpha=`expr $alpha + 1`;;
esac
n=`expr $n + 1`
done
special=`expr $c - $s - $alpha`
echo Words=$w
echo Characters=$c
echo Spaces=$s
echo Special symbols=$special

Code for Write a script to find the value of one number raised to the power of
another in Unix / Linux
echo "Input number"
read no
echo "Input power"
read power

counter=0
ans=1

while [ $power -ne $counter ]


do
ans=`expr $ans \* $no`
counter=`expr $counter + 1`
done

echo "$no power of $power is $ans"

S-ar putea să vă placă și