Record UNIX Shell Programming
Record UNIX Shell Programming
Start
Display
"Sum of n natural numbers"
Initialize i = 0, sum = 0
.i < n ?
Display "Sum of n
natural numbers are: $sum"
sum = sum + i
i = i +1
Stop
OUTPUT
$ sh sum.sh
AIM
To write a shell script to find the sum of n numbers.
ALGORITHM
Step 1: Start the program
Step 2: Enter the value of n
Step 3: Declare the variables of i and sum and both are initialized to zero.
Step 4: Perform the addition of n numbers using while loop
Step 5: Print the sum.
Step 6 : Stop the program
PROGRAM
echo "Sum of n natural numbers \n”
echo "Enter the value of n "
read n i = 0
sum = 0
while[$i –lt $n] do sum=‟expr $sum + expr $i‟ i = „expr $i + 1‟
done
echo "Sum of n natural numbers are : $sum”
Result:
Thus, the shell script to find the sum of n natural numbers is entered and its output was
verified.
FLOWCHART
Start
c=a
a=b
b=c
Stop
OUTPUT
Enter a 10
Enter b 20
After swapping 20 10
Expt. No. 02 Page No.: 03
AIM
To write Unix shell program to swap two given numbers
ALGORITHM
1. Start the program.
2. Read the variables a, b.
3. Interchange the values of a and b using another temporary variable c as follows:
a. c = a a = b b = c
4. Print the a and b.
5. Program Stop the program.
PROGRAM
echo “swapping using temporary variable” echo “enter a”
read a
echo “enter b” read b
c=$a a=$b b=$c
echo “after swapping” echo “$a”
echo “$b”
Result
Thus, the shell script to swap two numbers is entered and its output was verified.
FLOWCHART
Start
a=y%
b = y % 100
c = y % 400
a == 0 and
b != 0 or c == 0 ? No
(Is the year a leap year?)
Yes
Stop
Stop
OUTPUT
Enter a year:
2000
Year is leap year
Expt. No. 03 Page No.: 04
AIM
To write a shell program to check whether the given year is leap year or not.
ALGORITHM
1. Program Start the program.
2. Read the year.
3. Check whether year%4,year%100,year%400 is zero.
4. If zero then print year is leap year.
5. Else print year is not leap year.
6. Stop the program.
PROGRAM
echo "Finding Leap Year"
echo "enter any year"
read y a=`expr $y % 4` b=`expr $y % 100` c=`expr $y % 400`
if [ $a -eq 0 -a $b -ne 0 -o $c -eq 0 ] then
echo "$y is a leap year " else
echo "$y is not a leap year " fi
Result
Thus, the shell script to check whether the given year is leap year or not is entered and
its output was verified.
FLOWCHART
START
Set f = 1
Is No
n >= 1?
f=f*n
n=n-1
STOP
Expt. No. 04 Page No.: 05
AIM
To Write shell program to find the factorial of given N value.
ALGORITHM
1. Program Start the program.
2. Read the number as n.
3. For every iteration until n<1 compute f=f*n
4. Print the factorial of the given number as f
5. Stop the Program
PROGRAM
echo “enter a positive number”
read n
f=1
until [ $n -lt 1 ] do
f=`expr $f \* $n` n=`expr $n - 1` done
echo “factorial is $f”
OUTPUT
Enter positive number 4
Factorial is 24
Result
Thus, the shell script to find the factorial of a given number is entered and its output was
verified.
FLOWCHART
START
sum = 0
Is No
n>0?
Yes
rem = n % 10
n = n / 10
STOP
AIM
To write a shell program for finding the sum of digits of a given number. Sum = 1 + 2 + 3
+ 4 +….+ N
ALGORITHM
1. Start the program.
2. Read the number as n.
3. Initialise sum=0
4. For every iteration n> 0
compute rem=n%10
n=n/10 sum=sum + rem
5. Print the sum of digits of the given number as sum.
6. Stop the program.
PROGRAM
echo “enter the number” read n
sum=0
while [ $n -gt 0 ] do
rem=`expr $n % 10` n=`expr $n / 10` sum=`expr $sum + $rem` done
echo “sum of digits is:$sum”
OUTPUT
Enter a number:1234
Sum of digits is:10
Result:
Thus, the shell script to find the sum the digits of a given number is entered and its
output was verified.
FLOWCHART
START
Is
a>b Yes
&
a > c?
STOP
Is b > c? No
STOP
Display "$b big"
STOP
Expt. No. 06 Page No.: 07
AIM:
To write a shell program for finding the greatest among three numbers.
ALGORITHM
1. Program Start the program.
2. Read the three numbers a, b, c.
3. Check whether a is greater than b and c.
4. If yes then print a is big.
5. Else check whether b is greater than c.
6. If yes then print b is big.
7. Else print c is big.
8. Stop the program.
PROGRAM
echo “enter a b c”
read a
read b
read c
if [ $a -gt $b ] && [ $a -gt $c ] then
echo “$a big”
elif [ $b -gt $c ] then
echo “$b big”
else echo “$c big”
OUTPUT
Enter a b c 10 20 30
C big
Result
Thus, the shell script to find the sum the digits of a given number is entered and its output was
verified.