Unix Lab Programs: 1) Unix Shell Program To Calculate Odd and Even Numbers in Given Range

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

UNIX LAB PROGRAMS

1) Unix Shell Program to calculate odd and even numbers in given


range
echo enter n value as range to calculate odd and even numbers.
read n
i=1
while [ $i -le $n ]
do
if [ `expr $i % 2` -eq 0 ]
then
echo even=$i
else
echo odd=$i
f
i=`expr $i + 1`
done
2) Write Shell Script program to find the biggest of 3 numbers.
echo Enter 3 numbers with spaces in between
read a b c
l=$a
if [ $b -gt $l ]
then
l=$b
f
if [ $c -gt $l ]
then
l=$c
f
echo Lagest of $a $b $c is $l

3) Code for Write a shell program to check whether a given string


is palindrome or not
echo -n input string
read num
length =`expr length $num`
while [ $length ne 0 ]
do
b=$b ` expr substr $no $length 1`
length= `expr $length 1`
done
echo Reverse String is $b
if [ $num=$b ]
then
echo Is Polindrome
else
echo Is not Polindrome
f

4) Write a shell script to change extension of an existing file.


echo "Enter frst fle name: "
read f1
if [ -f $f1 ]
then
echo "Enter new extention: "
read e
cp ${f1} ${f1}.$e
echo $f1"."$e" fle is created"else
echo "File not Exist "
f

5) Reverse rows and columns of matrix using Shell Script code


echo "Enter Number of rows"
read r
echo "Enter Number of columns"
read c
i=0
echo "Enter elements"
until [ $i -eq `expr $r \* $c` ]
do
read a[$i]
i=`expr $i + 1`
done
i=0 ; k=0
echo "Transpose of a Matrix"
until [ $i eq $c ]
do
j=0;
until [ $j eq $r ]
do
n= `expr $j \* $c`
m= `expr $n + $i
b[$k]=${a[$m]}
echo "${b[$k]} \t"
k=`expr $k + 1`
j=`expr $j + 1`
done
i=`expr $i + 1`
echo "\n"
done

You might also like