0% found this document useful (0 votes)
103 views

Shell Programs

1. This document provides examples of Bash shell scripts to perform common mathematical and logical operations: calculating sums, averages, and products of integers; swapping values of two variables; calculating powers of a number; determining if a year is a leap year; determining if a number is odd or even; determining if a number is prime; calculating greatest common divisor of two numbers; reversing digits of a number; and calculating sum of digits of a number. 2. The examples demonstrate using basic Bash commands like expr, read, if/else conditional statements, while loops, arithmetic operations, and variables to process user input and perform calculations. 3. Step-by-step instructions and sample outputs are provided for each script to
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

Shell Programs

1. This document provides examples of Bash shell scripts to perform common mathematical and logical operations: calculating sums, averages, and products of integers; swapping values of two variables; calculating powers of a number; determining if a year is a leap year; determining if a number is odd or even; determining if a number is prime; calculating greatest common divisor of two numbers; reversing digits of a number; and calculating sum of digits of a number. 2. The examples demonstrate using basic Bash commands like expr, read, if/else conditional statements, while loops, arithmetic operations, and variables to process user input and perform calculations. 3. Step-by-step instructions and sample outputs are provided for each script to
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Sum and average of four integers


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

2. SWAP TWO NUMBERS USING TWO VARIABLES echo SWAP TWO NUMBERS USING TWO VARIABLES echo Enter the value of two numbers read a read b echo Enter the value of two numbers echo a=$a , b=$b a=$ [ $a + $b ] b=$ [ $a - $b ] a=$ [ $a - $b ] echo After swapping the values are: echo a=$a , b=$b Output: Enter the value of two numbers a=5 b=10 After swapping the values are: a=10 b=5 (or)
Swap two numbers using two variables echo "Enter value for x : " read x echo "Enter value for y : " read y echo "Before swap, x = $x and y = $y" z=$x x=$y y=$z echo "After swap, x = $x and y = $y

3.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"

4.Leap year or not

store year yy=0 isleap="false" echo -n "Enter year (yyyy) : " read yy # find out if it is a leap year or not if [ $((yy % 4)) -ne 0 ] ; then : # not a leap year : means do nothing and use old value of isleap

elif [ $((yy % 400)) -eq 0 ] ; then # yes, it's a leap year isleap="true" elif [ $((yy % 100)) -eq 0 ] ; then : # not a leap year do nothing and use old value of isleap else # it is a leap year isleap="true" fi

if [ "$isleap" == "true" ]; then else echo "$yy is NOT leap year" fi echo "$yy is leap year"

5.odd or even echo n Enter 3umber : read n rem=$(( $n % 2 )) if [ $rem eq 0 ] then echo $n is even number else echo $n is odd number fi 6.Prime number or not
echo -n "Enter a number: " read num i=2 while [ $i -lt $num ] do if [ `expr $num % $i` -eq 0 ] then echo "$num is not a prime number" echo "Since it is divisible by $i" exit fi i=`expr $i + 1` done echo "$num is a prime number " Output: [root@venu ]# ./prime1.sh Enter a number: 1879 1879 is a prime number [root@venu ]# ./prime1.sh Enter a number: 119

119 is not a prime number Since it is divisible by 7

7.GCD of two numbers 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

8.GCD of two positive numbers

echo -n "Enter First Number : " read n echo -n "Enter Second Number : " read m while [ $n -gt $m ] if [ $n -gt $m ] n=`expr $n - $m ` else m=`expr $m - $n ` wend echo "Greates Common Divisor = " $n 9.Reversing the digits of a number echo "Enter the number" read n sd=0 rev=0 while [ $n -gt 0 ]; do sd=$(( $n % 10 ))

rev=$(( $rev *\ 10 + $sd )) n=$(( $n / 10 )) done echo "Reverse of entered digit is $rev"
(Or) echo -n "Enter number : " read n # store single digit sd=0 # store number in reverse order rev="" # store original number on=$n # use while loop to caclulate the sum of all digits while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) # get next digit

# store previoues number and current digit in rev rev=$( echo ${rev}${sd} ) done echo "$on in a reverse order $rev"

10.sum of digits of a given number

echo -n "Enter number : " read n # store single digit sd=0 # store number of digit sum=0 # use while loop to caclulate the sum of all digits while [ $n -gt 0 ] do sd=$(( $n % 10 )) # get Remainder n=$(( $n / 10 )) # get next digit

sum=$(( $sum + $sd )) # calculate sum of digit done echo "Sum of all digit is $sum"

You might also like