Vous êtes sur la page 1sur 2

Shell Program Lab

Submitted by :- Sanjay Agal

Write a shell program to find the average of students marks using for loop and while
loop.

suppose file is of form "Name Surname Grade"

#!/bin/zsh

sum=0
min=10000
max=0
n=5
i=0
file="students.txt"

cat $file | while read line; do


set line
grade=$3
sum='expr $sum + $grade'
if [ $grade -gt $max ]; then
max=$grade
fi
if [ $grade -lt $min ]; then
min=$grade
fi
i='expr $i + 1'
if [ $i -ge $n ]; then
break
fi
done

average='expr $sum / $i'


echo "Max: $max Min: $min Avg: $average"

Write a shell program to convert temperature in centigrade to Fahrenheit.


echo "*** Converting between the different temperature scales ***"
echo "1. Convert Celsius temperature into Fahrenheit"
echo "2. Convert Fahrenheit temperatures into Celsius"
echo -n "Select your choice (1-2) : "
read choice
 
if [ $choice -eq 1 ]
then
echo -n "Enter temperature (C) : "
read tc
# formula Tf=(9/5)*Tc+32
tf=$(echo "scale=2;((9/5) * $tc) + 32" |bc)
echo "$tc C = $tf F"
lif [ $choice -eq 2 ]
then
echo -n "Enter temperature (F) : "
read tf
# formula Tc=(5/9)*(Tf-32)
tc=$(echo "scale=2;(5/9)*($tf-32)"|bc)
echo "$tf = $tc"
lse
echo "Please select 1 or 2 only"
exit 1
fi

Write a shell script that accepts a file name starting and line number as arguments and
display all line.
#!/bin/csh
head -$argv[3] $argv[1] | tail -n $argv[2]

Write a shell script to find the area of the triangle.


# Area=(1/2) x Base x Height
echo -n "Enter base of a triangle : "
read b
 
echo -n "Enter height of a triangle : "
read h
# calculate it and display back
area=$(echo "scale=2;(1/2) * $b * $h"|bc)
echo "Area of a triangle is $area"

Write a shell script that accept two integers as its argument and computes the value of
first number raised to the power of the second number.
#!/bin/csh
set c = 1
set result = 1
while($c <= $argv[2])

@ result *= $argv[1]

@ c = $c + 1
end
echo "$argv[1] raised to the power of $argv[2]=$result"

Vous aimerez peut-être aussi