shell programs

Share Embed Donate


Short Description

this is important shell programs in computer practices lab ii covered under anna university...

Description

UNIX SHELL PROGRAMMING 1. Unix Shell Shell program programss u using sing Expressio Expressions ns a. Write a Shell program to find the area and circumference of a circle .

2. Unix Shell Shell progr programs ams using using Condi Condition tional al Statements a. Write Write a Shell Shell program program to find find the largest among three numbers. b. Write Write a Shel Shelll progr program am to to check check the the given number is even or odd. c. Wr Writ itee a Shel Shelll progr program am to to chec check  k  whether given year is leap or not. no t. d. Write Write a She Shell ll program program to disp display lay student grades. e. Wr Writ itee a Shel Shelll progr program am to to find find the the roots of a quadratic equation.

f. Write Write a Shel Shelll pro progr gram am to execut executee various UNIX commands using case statements. 3. Unix Shell Shell progra programs ms using using loopi looping ng Statem Statements. ents. a. Write Write a Shell Shell program program to find find the factorial of a number using for loop. b. Write Write a Shel Shelll progr program am to to gener generate ate Fibonacci series. c. Write Write a Shell Shell program program to gene generat ratee prime numbers between 1 and 50. d. Write Write a Shel Shelll progr program am to to check check the the given integer is prime or not e. Write Write a Shell Shell program program to prin printt the the reverse of a number. f. Wr Writ itee a She Shell ll progr program am to to chec check k the the given string is palindrome or not. g. Write Write a She Shell ll program program to chec check k the the given integer is Armstrong number or not. h. Write Write a She Shell ll program program to find find the sum of square of individual digits of of a number. i. Wr Writ itee a She Shell ll progr program am to fin find d the the factorial of a number using for loop.

 j.

Write a Shell Shell program to to find sum of  individual digits of a number. k. Write Write a Shel Shelll progr program am to to find find the the sum sum of digits of a number until a single digit is obtained. l. Writ Writee a Shel Shelll prog progra ram m to find find the the largest digit of a number. m. Write Write a Shell Shell progra program m to find the smallest digit of a number. n. Write Write a Shel Shelll progr program am to to find find the the second largest digit from a number.

o. Write Write a Shell Shell progr program am to to find find the the sum sum of all numbers between 50 and 100, which are divisible by 3 and not divisible by 5.  p. Write a Shell Shell program to count the number of vowels in a line of text. 4. Unix Unix shell shell progr programs ams usin using g arrays arrays.. a. Write Write a Shell Shell progr program am to to sort sort ‘n’ ‘n’ different numbers. b. Write Write a Shell Shell program program to find find the largest and smallest among ‘n’ different numbers

c. Write Write a Shell Shell progr program am to to find find the the sum sum of ‘n’ different numbers. d. Write Write a Shell Shell program program to find find the sum of odd and even from a set of  numbers.

5. Unix Unix shell shell pro progr grams ams usin using g Funct Function ionss a. Write Write a Shell Shell program program to find find the largest number between two numbers using function.

 b. Write a Shell Shell program to find the sum sum of two numbers using function  programming.  programming. c. Writ Writee a She Shell ll progr program am to to find find factorial of a number using recursive function.

#Ex.No - 1a : To find the area and circumference of a circle. #circle.sh echo -n "Enter Radius of the Circle : " read r  area=$(echo "3.14*$r*$r"|bc -1 ) cf=$(echo "2*3.14*$r"|bc -l ) echo "=========================== "============================" =" echo "AREA OF CIRCLE : $area" echo "CIRCUMFERENCE : $cf" echo "=========================== "============================" =" OUTPUT:

[CPL_2_lab@localhost SH]$ sh circle.sh Enter Radius of the Circle : 7 ============================ AREA OF CIRCLE : 153.86 CIRCUMFERENCE : 43.96 ============================ [CPL_2_lab@localhost SH]$

#Ex.2a Greatest of three numbers #largest3.sh echo "Enter Three Numbers A,B,C" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "$a is greater" elif [ $b -gt $c ] then echo "$b is greater" else echo "$c is greater" fi OUTPUT:

[CPL_2_lab@localhost SH]$ sh large3.sh Enter Three Numbers A,B,C 78 79 90 90 is greater  [CPL_2_lab@localhost SH]$

#Ex.No.2b Check odd or even number #check_even.sh

echo -n "Enter the number : " read num if [ $(($num%2)) -eq 0 ] then echo "The number $num is even" else echo "The number $num is odd" fi OUTPUT:

[CPL_2_lab@localhost SH]$ sh check_even.sh Enter the number : 235 The number 235 is odd [CPL_2_lab@localhost SH]$ #Ex.No.2c: Check Leap year or not #check_leap.sh echo -n "Enter the year : " read year  n=$(($year%4)) if [ $n -eq 0 ] then echo "$year is Leap year" else echo "$year is not leap year" fi OUTPUT:

[CPL_2_lab@localhost SH]$ sh check_leap.sh Enter the year : 1996 1996 is Leap year  [CPL_2_lab@localhost SH]$

#Ex.No.2d Display Student grade #grade.sh echo "Enter Marks in five subjects" read m1 m2 m3 m4 m5 tot=$(($m1+$m2+$m3+$m4+$m5)) average=$(echo "$tot/5"|bc -l) avg=$average echo "$avg" if [ $m1 -lt 50 -o $m2 -lt 50 -o $m3 -lt 50 -o $m4 -lt 50 -o $m5 -lt 50 ] then grade="FAIL"

elif [ $(echo "$avg >= 75"|bc) -eq 1 ] then grade="FIRST CLASS WITH DISTINCTION" elif [ $(echo "$avg >= 60"|bc) -eq 1 ] then grade="FIRST CLASS" elif [ $(echo "$avg >= 50"|bc) -eq 1 ] then grade="SEOCND CLASS" else grade="THIRD CLASS" fi echo "=========================" echo "TOTAL MARKS : $tot" echo "AVERAGE MARKS : $avg" echo "GRADE : $grade" echo "=========================" OUTPUT:

[CPL_2_lab@localhost SH]$ sh grade.sh Enter Marks in five subjects 95 90 75 68 82 82.00 ================================== TOTAL MARKS : 410 AVERAGE MARKS : 82.00 GRADE : FIRST CLASS WITH DISTINCTION ================================== [CPL_2_lab@localhost SH]$

#Ex.No.2e.Roots of Quadratic Equation #quad_roots.sh echo "Enter the Co-ordinates A, B, C : " read a b c d=`expr $b \* $b - 4 \* $a \* $c` echo "================================== " if [ $d -lt 0 ] then echo "Roots are Imaginary..!" elif [ $d -eq 0 ] then echo "Roots are equal..!" r1=`expr -1 \* $b / 2 \* $a ` r2=$r1 echo "The Roots are R1 = $r1 R2 = $r2"

else echo "Roots are Different..!" r=$(echo "scale=2;sqrt($d)" | bc) x=$(echo "-1*$b"|bc) r1=$(echo "scale=2;($x+$r)/(2*$a)"|bc) r2=$(echo "scale=2;($x-$r)/(2*$a)"|bc) echo "The Roots are R1 = $r1 R2 = $r2" fi echo "================================== " OUTPUT: [CPL_2_lab@localhost SH]$ sh quad_roots.sh Enter the Co-ordinates A, B, C : 1 -3 -4 ================================== Roots are Different..! The Roots are R1 = 4.00 R2 = -1.00 ================================== [CPL_2_lab@localhost SH]$

#Ex.No:3a.Find Factorial using for loop #fact.sh echo -n "Enter the Number : " read n f=1 for((i=1;i
View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF