0% found this document useful (0 votes)
80 views6 pages

Pgms

This document contains summaries of several shell scripts that perform various tasks: 1) A case statement script that prints a message based on a variable value. 2) A script that counts vowels in a word and consonants. 3) A script that calculates the sum, average, and product of four integers. 4) Additional scripts perform calculations for simple interest, swapping variables, checking for leap years, and more.

Uploaded by

Hari Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views6 pages

Pgms

This document contains summaries of several shell scripts that perform various tasks: 1) A case statement script that prints a message based on a variable value. 2) A script that counts vowels in a word and consonants. 3) A script that calculates the sum, average, and product of four integers. 4) Additional scripts perform calculations for simple interest, swapping variables, checking for leap years, and more.

Uploaded by

Hari Krishnan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Case statement

FRUIT="kiwi" case "$FRUIT" in "apple") echo "Apple pie is quite tasty." ;; "banana") echo "I like banana nut bread." ;; "kiwi") echo "New Zealand is famous for kiwi." ;; esac

vowels
echo 'Give a word' read word case $word in a*|e*|i*|o*|u*) vowels=&((klinker + 1 )) ;; *) consonants=&((medeklinker + 1 )) ;; esac echo $vowels echo $consonants word=abstemiously vowels=$(echo $word | sed 's/[^aeiou]//g') echo "${#vowels} vowels"

SUM AND AVG


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

SI
echo " Enter the principle value: " read p echo " Enter the rate of interest:" read r echo " Enter the time period:" read t s=`expr $p \* $t \* $r / 100` echo " The simple interest is " echo $s

SAWP
echo read echo read echo z=$x x=$y y=$z echo "Enter value for x : " x "Enter value for y : " y "Before swap, x = $x and y = $y"

"After swap, x = $x and y = $y"

LEAP YEAR
Echo -n Enter the year(yyyy) to find leap year :- Read year d = `expr $year % 4` b = `expr $year % 100` c = `expr $year % 400` if [ $d -eq 0 -a $b -ne 0 -o $c -eq 0 ] then Echo year is leap year else Echo not leap year fi

ODD or EVEN

echo -n "Enter numnber : " read n rem=$(( $n % 2 )) if [ $rem -eq 0 ] then echo "$n is even number" else echo "$n is odd number" fi

Positive or Negative

if [ $1 -gt 0 echo "$1 is elif [ $1 -lt then echo "$1 is elif [ $1 -eq then echo "$1 is else echo "Opps! fi

]; then positive" 0 ] negative" 0 ] zero" $1 is not number, give number"

Sum of digits of a number

echo -n "Enter number : " read n sd=0 sum=0 while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get n=$(( $n / 10 )) # get sum=$(( $sum + $sd )) # done echo "Sum of all digit is

Remainder next digit calculate sum of digit $sum"

Even numbers print


Echo -n enter any integer number to find even and odd :- Read number rem = `expr $number % 2` if test $rem -eq 0 then Echo number is even else Echo number is odd fi

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

DIvisible by 5 or not
echo "Enter a number" read number if [ $(( $number % 5 )) -eq 0 ] ; then

echo "Your number is divisible by 5" else echo "Your number is not divisible by 5" fi

q&r echo program for Q & R echo Enter the first number read a echo Enter the second number read b quo=`expr $a / $b` rem=`expr $a % $b` echo The quotient is:$quo

echo The remainder is:$rem done

POWER OF X echo read echo read "Input number" no "Input power" 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"

STUDENT GRADES echo "Enter the five subject marks for the student" read m1 m2 m3 m4 m5 sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5` echo "Sum of 5 subjects are: " $sum1 per=`expr $sum1 / 5` echo " Percentage: " $per if [ $per -ge 60 ] then echo "You get Distinction elif [ $per -ge 50 ] then echo You get First class elif [ $per -ge 40 ] then echo "You get Second class" else echo "You get Fail" fi

Program for Factorial of a given number


echo Enter a number to find its factorial read n i=1 f=1 if test $n eq 0 then echo Factorial is 1 while test $i le $n do f=$(( $f * $i )) i=$(( $i + 1 )) done echo Factorial is $f fi

You might also like