Os Lab 2 - Shell Scripting
Os Lab 2 - Shell Scripting
Variables: Shell scripts can define variables to store data. Variables are declared
without any specific type and can hold numeric or string values. Variable names
are case-sensitive and conventionally use uppercase letters.
Example: name="John"
Input and Output: Shell scripts can read input from the user or from files and
can write output to the screen or files. The 'read' command is used to read user
input, and 'echo' is used to print output.
Control Structures: Shell scripts support various control structures, such as if-
else statements and loops, for decision-making and repetitive tasks.
Example (loop):
Functions: Shell scripts can define functions to encapsulate reusable code blocks.
Functions can take parameters and return values.
BIGGEST OF THE THREE NUMBERS
ALGORITHM:
Step 1: Start The Problem
Step 2: Take Three Inputs From The User
Step 3: In If-Else Condition, Check Which Is The Greatest
Step 4: Also Check With The Third Number
Step 5: Find The Result
Step 6: Print The Result
Step 7: Stop The Program
PROGRAM
ALGORITHM:
Step 1 : Get a number
Step 2 : Use do-while loop to compute the factorial by using the below formula
Step 3 : fact(n) = n * n-1 * n-2 * .. 1
Step 4 : Display the result.
Code Explanation:
to enter a number and reads the input into the variable num
echo "Enter a number"
read num
The variable fact is initialized with a value of 1. This variable will be used to calculate the factorial
fact=1
This while loop calculates the factorial of the number entered by the user. The loop continues as
long as the value of num is greater than 1
while [ $num -gt 1 ]
do
fact=$((fact * num))
num=$((num - 1))
done
Finally, the factorial value stored in the variable fact is displayed using the echo command
echo Factorial=$fact