32 Shell Programming codes

23
Shell Programming 1. Write a shell script to swap two numbers without using 3rd variable. echo Enter valuefor a: read a echo Enter valuefor b: read b clear echo Values of variables before swaping echo A=$a echo B=$b echo Values of variables after swaping a=`expr $a + $b` b=`expr $a - $b` a=`expr $a - $b` echo A=$a echo B=$b 2. Write a shell script to read the marks of a Student and print the grade. 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

description

shell programs

Transcript of 32 Shell Programming codes

Shell Programming

1. Write a shell script to swap two numbers without using 3rd variable.

echo Enter valuefor a:read aecho Enter valuefor b:read bclearecho Values of variables before swapingecho A=$aecho B=$becho Values of variables after swapinga=`expr $a + $b`b=`expr $a - $b`a=`expr $a - $b`echo A=$aecho B=$b

2. Write a shell script to read the marks of a Student and print the grade.

echo "Enter the five subject marks for the student"read m1 m2 m3 m4 m5sum1=`expr $m1 + $m2 + $m3 + $m4 + $m5`echo "Sum of 5 subjects are: " $sum1per=`expr $sum1 / 5`echo " Percentage: " $perif [ $per -ge 60 ]thenecho "You get Distinctionelif [ $per -ge 50 ]thenecho You get First classelif [ $per -ge 40 ]thenecho "You get Second class"else echo "You get Fail"fi

3. Write a shell script to read two integer numbers and perform basic arithmetic operations based on users choice (use case structure).

clearsum=0i="y"

echo " Enter one no."read n1echo "Enter second no."read n2while [ $i = "y" ]doecho "1.Addition"echo "2.Subtraction"echo "3.Multiplication"echo "4.Division"echo "Enter your choice"read chcase $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";;esacecho "Do u want to continue ?"read iif [ $i != "y" ]then exitfidone

4. Write a shell script to find the sum of first N Natural Numbers (use while structure)

echo "Sum of n natural numbers \necho "Enter the value of n "read ni = 0sum = 0while[$i lt $n]dosum=expr $sum + expr $ii = expr $i + 1doneecho "Sum of n natural numbers are : $sum

5. Write a shell script to find the sum of first N numbers in Fibonacci series (use for structure)

echo enter the numberread ni=1f=0s=1next=`expr $f + $s`echo $fecho $secho $nextwhile [ $i -le `expr $n - 3` ]dof=$ss=$nextnext=`expr $f + $s`echo $nexti=`expr $i + 1`done

6. Write a shell script to print a given number in reverse order and sum of the individual digits.

echo enter the numberRead nrev=0s=0While [ $n gt 0 ]dor=`expr $n % 10`n=`expr $n / 10`rev=`expr $rev \* 10 +$r`doneecho the reverse of the given number is $revwhile [ $rev gt 0 ]dot= `expr $rev % 10 `s= `expr $s + $t `rev=`expr $rev/10`doneecho sum of digits is $s

1. Write a Shell program to check the given number is even or odd. PROGRAMecho "Enter a number:" read nif [ `expr $n % 2` = 0 ] thenecho "Even number"elseecho "Odd number"fi-bash-3.2$sh evenodd.sh Enter a number:6Even numberOUTPUT-bash-3.2$sh evenodd.sh Enter a number:83Odd number2. Write a Shell program to check and display 10 leap years. PROGRAMfor((i = 2000 ; i{>if [ $1-gt$2 ]>then>echo "$1 is greater">else>echo "$2 is greater">fi>}OUTPUT-bash-3.2$largest 10 20 20 is greater-bash-3.2$largest 20 10 20 is greater / SL / CSE / - 17

25. Write a Shell program to find the largest among three numbers.PROGRAMecho "Enter the first number:" read aecho "Enter the second number:" read becho "Enter the third number:" read cif [ $a-gt$b-a$a-gt$c ] thenecho "$s is greater" elif [ $b-gt$c ]thenecho "$b is greater"elseecho "$c is greater"fiOUTPUT-bash-3.2$sh larthree.sh Enter the first number: 20Enter the second number: 30Enter the third number: 1030 is greater-bash-3.2$26. Write a Shell program to find the largest among n different numbers.PROGRAMecho "Enter the number of elements:" read nl=0for((i = 1 ; i