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

Os Lab 2 - Shell Scripting

Shell scripts are text files that contain a series of commands executed by the shell to automate tasks. They use extensions like .sh and include shell keywords, commands, functions, and control flows. Variables store data and input/output is used to interact. Functions encapsulate reusable code and control structures like if/else and loops are used for decisions and repetition.

Uploaded by

llm2k23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Os Lab 2 - Shell Scripting

Shell scripts are text files that contain a series of commands executed by the shell to automate tasks. They use extensions like .sh and include shell keywords, commands, functions, and control flows. Variables store data and input/output is used to interact. Functions encapsulate reusable code and control structures like if/else and loops are used for decisions and repetition.

Uploaded by

llm2k23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Shell Scripting

A shell script is a text file containing a series of commands that are


executed in sequence by the shell. Shell scripts are used to automate tasks and
perform system administration tasks.
Each shell script is saved with `.sh` file extension e.g., myscript.sh.

A shell script comprises the following elements –

Shell Keywords – if, else, break etc.


Shell commands – cd, ls, echo, pwd, touch etc.
Functions -
Control flow – if..then..else, case and shell loops etc.

Writing a Shell Script:

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

Prompt the user to enter three numbers


echo "Enter three Numbers:"
read a b c
This line reads the user's input and assigns the values to the variables a, b, and c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "$a is Greatest"
Compare b with a and c
elif [ $b -gt $c ] && [ $b -gt $a ]
then
echo "$b is Greatest"
If the above conditions are not met, then c is the greatest
else
echo "$c is Greatest!"
fi
OUTPUT: Open Ubuntu application in Windows or open the terminal in ubuntu.

Open new shell file using vi editor

Enter the code after pressing the insert key


After coding, press “esc” key followed by :wq

Run the shell file using bash command bash filename.sh


FACTORIAL OF A GIVEN NUMBER

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

You might also like