Вы находитесь на странице: 1из 13

Ex.NO.

SHELL PROGRAMMING

AIM: To understand shell programming by using conditional statements, looping and functions.

2.1 SIMPLE SHELL PROGRAMMING THE AREA AND CIRCUMFERENCE OF A CIRCLE Aim: To write a Shell program to find the area and circumference of a circle Algorithm: Step 1: Start the program. Step 2: Read radius (r) Step 3: Compute the area of a circle using the formula: 3.14*r*r Step 4: Compute the circumference of the circle using the formula: 2*3.14*r Step 5: Print area and circumference of the circle Step 6: Stop. Program: echo "Enter the radius" read r area=`expr "3.14 * $r * $r" | bc` cir=`expr "2 * 3.14 * $r" | bc` echo "The area is " $area echo "The circumference is " $cir Output: Enter the radius 25 The area is 1962.50 The circumference is 157.00 Result: Thus the shell program to find the area and circumference of a circle was executed and verified successfully.

2.2 CONDITIONAL STATEMENTS ODD OR EVEN Aim: To write a Shell program to check the given number is even or odd Algorithm: Step 1: Start the program. Step 2: Read a number (n) Step 3: Check if (n%2) =0 Step 3.1: Print The given number is even, else Step 4: Print The given number is odd. Step 5: Stop. Program: echo "Enter the number" read n a=`expr $n % 2` if [ $a -eq 0 ] then echo "$n is even" else echo "$n is odd" fi Output: Enter the number 12 12 is even Result: Thus the shell program to check the given number is even or odd was executed and verified successfully.

LEAP YEAR OR NOT Aim: To write a Shell program to check the given year is leap year or not Algorithm: Step 1: Start the program. Step 2: Read a year (y) Step 3: Check if (y%4) =0 Step 3.1: Print The given year is a leap year, else Step 4: Print The given year is not a leap year. Step 5: Stop. Program: echo "Enter the year" read y b=`expr $y % 4` if [ $b -eq 0 ] then echo "$y is a leap year" else echo "$y is not a leap year" fi Output: Enter the year 2012 2012 is a leap year Result: Thus the shell program to check the given year is leap year or not was executed and verified successfully.

LARGEST AMONG THREE NUMBERS Aim: To write a Shell program to find the largest among three numbers Algorithm: Step 1: Start the program Step 2: Read a,b,c Step 3: Check if (a>b) and (a>c) then Step3.1: Print The biggest value is a else Step 4: if (b>c) then Step4.1: Print The biggest value is b else Step4.2: Print The biggest value is c Step 5: Stop the program Program: echo "Enter Three Numbers" 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: Enter Three Numbers 12 45 34 45 is Greater Result: Thus the shell program to find the largest among three numbers was executed and verified successfully.

EXECUTE VARIOUS UNIX COMMANDS USING CASE STATEMENTS Aim: To write a Shell program to execute various UNIX commands using case statements Algorithm: Step 1: Start the program. Step 2: Display menu. Step 3: Enter your choice using case-esac statement. Step 4: If your choice 1, display the current working directory Step 5: If your choice 2, display the current system date and time. Step 6: If your choice 3, display the information about all the users who have logged on to the system. Step 7: If entered case option is invalid code, then print Invalid choice. Step 8: Stop. Program: echo "Menu" echo "1.Your current directory" echo "2.Today's date" echo "3.List of user's Logged in" echo "Your choice" read choice case $choice in 1) pwd;; 2) date;; 3) who;; *) echo "Invalid choice";; esac Output: Menu 1.Your current directory 2.Today's date 3.List of user's Logged in Your choice 1 /home/sony Result: Thus the shell program to execute various UNIX commands using case statements was executed and verified successfully.

SHELL PROGRAM TO DISPLAY STUDENT GRADES Aim: To write a Shell program to display student grades Algorithm: Step 1: Start the program. Step 2: Read the percentage per. Step 3: Check if ( per 75 ) then Print grade is distinction, else Step 4: Check if ( per 60 ) then Print grade is first class, else Step 5: Check if ( per 45 ) then Print grade is second class, else Step 6: Print grade is fail. Step 7: Stop. PROGRAM echo "Enter the percentage" read per if [ $per -ge 75 ] then echo "Grade is distinction" elif [ $per -ge 60 ] then echo "Grade is first class" elif [ $per -ge 45 ] then echo "Grade is second class" else echo "Grade is fail" fi Output: Enter the percentage 75 Grade is distinction Result: Thus the shell program to display student grades was executed and verified successfully.

2.3 TESTING AND LOOPS PALINDROME OR NOT Aim: To write a Shell program to check the given number is palindrome or not Algorithm: Step 1: Start the program Step 2: Read a number (a) Step 3: Assign da Step 4: Assign c0, b1 Step 5: Set a loop, the number (a) is greater than 0 Step 5.1: b=a%10 Step 5.2: c=c*10+b Step 5.3: a=a/10 Step 6: After the end of the loop, check whether the value of d is equal to c, if equal to c, then print The given number is a palindrome Step 7: If not equal to c, then print The given number is not a palindrome Step 8: Stop PROGRAM echo "Enter a number" read a d=$a c=0 b=1 while [ $a -gt 0 ] do b=`expr $a % 10` c=`expr $c \* 10 + $b` a=`expr $a / 10` done if [ $d -eq $c ] then echo "PALINDROME" else echo "NOT PALINDROME" fi Output: Enter a number 121 PALINDROME Result: Thus the shell program to check the given number is palindrome or not was executed and verified successfully.

PRIME OR NOT Aim: To write a Shell program to check the given integer is prime or not. Algorithm: Step 1: Start the program. Step 2: Read a number (n) Step 3: Assign in-1, t=0 Step 4: Set a while loop up to the value of i is greater than or equal to 2 Step 4.1: p=n%i Step 4.2: Check if (p =0) then t=t+1 Step 4.3: i=i-1 Step 4.4: Goto Step 4 Step 5: After the execution of the loop, check if (t>0), then Print The given number is not a prime number else Step 6: Print The given number is a prime number Step 7: Stop. PROGRAM echo "Enter a Number" read n i=`expr $n - 1` t=0 while [ $i -ge 2 ] do p=`expr $n % $i` if [ $p -eq 0 ] then t=`expr $t + 1` fi i=`expr $i - 1` done if [ $t -gt 0 ] then echo "The Number $n is not a Prime Number" else echo "The Number $n is a Prime Number" fi Output: Enter a Number 17 The Number 17 is a Prime Number Result: Thus the shell program to check the given integer is prime or not was executed and verified successfully.

THE SUM OF SQUARE OF INDIVIDUAL DIGITS OF A NUMBER Aim: To write a Shell program to find the sum of square of individual digits of a number Algorithm: Step 1: Start the program. Step 2: Read a number (a) Step 3: Initialize the value for b=1 and c=0 Step 4: Set a while loop up to the number is greater than zero. Step 5: Using modulus operator, separate the digits in the given number Step 6: Find the square of each digits in the given number. Step 7: Find the sum of the squares Step 8: After the execution of the loop, print The sum of square of digits of a number Step 9: Stop PROGRAM echo "Enter the number" read a b=1 c=0 while [ $a -gt 0 ] do b=`expr $a % 10` c=`expr $c + $b \* $b` a=`expr $a / 10` done echo "The sum of square of digits is" $c Output: Enter the number 15 The sum of square of digits is 26 Result: Thus the shell program to find the sum of square of individual digits of a number was executed and verified successfully.

THE SUM OF DIGITS OF A NUMBER Aim: To write a Shell program to find the sum of digits of a number Algorithm: Step 1: Start the program. Step 2: Enter a number. Step 3: Set a loop up to the number is not equal to zero. Step 4: Compute a num % 10 Step 5: Compute sum sum + a Step 6: Compute num num / 10 Step 7: After the end of the loop, print the sum of digits. Step 8: Stop. PROGRAM echo "Enter the number" read num sum=0 while [ $num -gt 0 ] do a=`expr $num % 10` sum=`expr $sum + $a` num=`expr $num / 10` done echo "The sum of digits is $sum" Output: Enter the number 132 The sum of digits is 6 Result: Thus the shell program to find the sum of digits of a number was executed and verified successfully.

THE FACTORIAL OF A NUMBER Aim: To write a Shell program to find the factorial of a number. Algorithm: Step 1: Start the program. Step 2: Enter a number. Step 3: Set a loop to find the factorial of the given number using the formula. fact = fact * 1 Step 4: Print the factorial of the given number. Step 5: Stop. Program: echo "Enter a number" read num i=1 fact=1 while [ $i -le $num ] do fact=`expr $fact \* $i` i=`expr $i + 1` done echo "The factorial value is $fact" Output: Enter a number 5 The factorial value is 120 Result: Thus the shell program to find the factorial of a number was executed and verified successfully.

FIBONACCI SERIES Aim: To write a Shell program to generate Fibonacci series. Algorithm: Step 1: Start the program. Step 2: Enter a number. Step 3: Initialize values for a -1, b 1, i 0 Step 4: Check whether the number is zero or not. If zero, print zero value. If not zero, go further. Step 5: Set a loop up to the given number. Step 6: Compute c a + b ab bc Step 7: Every increment in the loop prints the value of c. Step 8: After the execution of the loop stops the program. Program: echo "Enter the limit read n echo "The Fibonacci series is" a=-1 b=1 i=0 while [ $i -lt $n ] do c=`expr $a + $b` a=$b b=$c echo "$c" i=`expr $i + 1` done Output: Enter the limit 5 The Fibonacci series is 0 1 1 2 3 Result: Thus the shell program to generate Fibonacci series was executed and verified successfully.

2.4 FUNCTIONS THE SUM OF TWO NUMBERS USING FUNCTION Aim: To write a Shell program to find the sum of two numbers using function Algorithm: Step 1: Create an user defined function with the name sum Step 2: Read the value of a and b Step 3: Compute sum a+b Step 4: Print the sum of two numbers. Step 5: Stop. Program: $ sum() >{ > echo "Enter the value of a and b" > read a b > sum=`expr $a + $b` > echo "The sum is $sum" >} Output: Enter the value of a and b 10 20 The sum is 30 Result: Thus the shell program to find the sum of two numbers using function was executed and verified successfully.

RESULT: Thus the shell program has been understood using using conditional statements, looping and functions was executed and verified successfully.

Вам также может понравиться