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

Record UNIX Shell Programming

The document outlines several shell scripts for basic programming tasks, including calculating the sum of natural numbers, swapping two numbers, checking for leap years, finding factorials, summing digits of a number, and identifying the greatest among three numbers. Each section includes a flowchart, an algorithm, the shell script code, and the output results. The aim of each experiment is clearly stated, demonstrating practical applications of shell scripting.

Uploaded by

cyberdomesoc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Record UNIX Shell Programming

The document outlines several shell scripts for basic programming tasks, including calculating the sum of natural numbers, swapping two numbers, checking for leap years, finding factorials, summing digits of a number, and identifying the greatest among three numbers. Each section includes a flowchart, an algorithm, the shell script code, and the output results. The aim of each experiment is clearly stated, demonstrating practical applications of shell scripting.

Uploaded by

cyberdomesoc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

FLOWCHART

Start

Display
"Sum of n natural numbers"

Display "Enter the value of n"

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

Sum of n natural numbers

Enter the value of n : 3

Sum of n natural numbers are : 6


Expt. No. 01 Page No.: 02

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

Display "Enter a"


Input a
Display "Enter b"
Input b

c=a
a=b
b=c

Display "After swapping"


Display a and b values

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

Display "Finding Leap Year"


Display "Enter any year”
Input y

a=y%
b = y % 100
c = y % 400

a == 0 and
b != 0 or c == 0 ? No
(Is the year a leap year?)

Display "$y is not a leap


year"

Yes
Stop

Display "$y is not a leap year"

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

Prompt "Enter a positive number"


Read n

Set f = 1

Is No
n >= 1?

Yes Print "Factorial is f"

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

Display "Enter the number"


Input n

sum = 0

Is No
n>0?

Yes

Display "Sum of digits is: $sum"

rem = n % 10
n = n / 10

STOP

sum = sum + rem


Expt. No. 05 Page No.: 06

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

Display "Enter a, b, c"


Input a,b,c

Is
a>b Yes
&
a > c?

No Display "$a big"

STOP

Is b > c? No

Display "$c big"


Yes

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.

You might also like