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

Task 2

The document discusses shell scripting and provides examples of shell script programs. It defines what a shell and shell script are. It then covers variables, input/output statements, arithmetic operations, decision making statements like if/else and case, loops like for, while and until. Finally, it provides 7 examples of shell script programs to find the factorial of a number, check if a number is even or odd, find the power of a number, check if a number is a palindrome, print the Fibonacci series, check if a number is prime, and check if a number is an Armstrong number.

Uploaded by

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

Task 2

The document discusses shell scripting and provides examples of shell script programs. It defines what a shell and shell script are. It then covers variables, input/output statements, arithmetic operations, decision making statements like if/else and case, loops like for, while and until. Finally, it provides 7 examples of shell script programs to find the factorial of a number, check if a number is even or odd, find the power of a number, check if a number is a palindrome, print the Fibonacci series, check if a number is prime, and check if a number is an Armstrong number.

Uploaded by

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

1 160120733057

TASK – 2 SHELL SCRIPTING


SHELL:
 Shell is a user program or its environment provided for user interaction.
 It is a command language interpreter that executes commands.
 It reads from the input from the user and executes programs based on the input
 It displays the program output when the program finishes the execution
 Shell is not part of system kernel, but uses the system kernel to execute programs, create
files, etc.

SHELL SCRIPT:
 A shell script is a list of commands in a computer program that is run by the UNIX shell which
is a command line interpreter.
 It usually has comments that describes the steps
 Operations performed by shell scripts are program execution, File manipulation and text
printing

VARIABLES:
 The shell enables us to create, assign and delete variables.
 Variable names must contain only letters, numbers or the underscore
 Defining Variables:
Variable_name = variable_value
 Variable can be accessed by using a dollar sign ($) as a prefix for variable_name
 After a variable is marked read-only, its value cannot be changed
 Unsetting a variable directs the shell to remove the variable from the list of the variables.
 Unset command cannot be used on the variables which are marked read-only

READ STATEMENT:
 The Read statement is used to get input from user and store the data to variable
 Syntax: read variable1, variable2, …., variable

ECHO STATEMENT:
 The Echo statement is used to display text or value of variables on the screen.
 Syntax: echo [options] [string, variables…]

BASIC SHELL SCRIPT:


 touch HelloWorld.sh ---> creates a file
 nano HelloWorld.sh ----> opens the file in text editor

 chmod +x HelloWorld.sh ----> gives permission to execute the script


 ./HelloWorld.sh runs the script

Output:
2 160120733057

ARITHMETIC OPERTIONS:
 Demonstration for basic Arithmetic operations:
Code:

Output:

DECISION MAKING STATEMENTS


 There are two types of decision-making statements within shell scripting. They are:
- If-else statement
- Case-esac statement
IF-ELSE STATEMENT:
 It is a conditional statement.
 There are couple of varieties present within the if-else statement. They are:
- If-fi
- If-else-fi
- If-elif-else-fi
- Nested if-else
 Syntax:
- If-fi
if [ expression ]; then
statements
fi
- If-else-fi
if [ expression ]; then
statement1
3 160120733057

else
statement2
fi
- If-elif-else-fi
if [ expression ]; then
statement1
elif [ expression ]
then
statement2
else
statement3
fi
- Nested if-else
if [ expression ]
then
statement1
if [ expression ]
then
statement
else
statement
fi
else
statement
fi
Demo Program for If-else Statement:

Output:

THE CASE-ESAC STATEMENT:


 Case-esac is basically working the same as switch statement in programming.
 Syntax:
4 160120733057

case $var in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
 Demo for case-esac statement:

Output:

LOOPS:

 There are 3 looping statements which can be used in bash programming


- While statement
- For statement
- Until statement

FOR STATEMENT:
 The for loop moves through a specified list of values until the list is exhausted.
 Syntax:
for varname in list
do
statement
done

WHILE STATEMENT:
 Linux scripting while loop is similar to C language while loop
 There is a condition in while and commands are executed till the condition is valid. Once
condition becomes false, loop terminates.
 Syntax:
while [condition]
do
statement
done

UNTIL STATEMENT:
5 160120733057

 The only difference is that until statement executes its code block while its conditional
expression is false, and while statement executes its code block while its conditional
expression is true
 Until loop always executes at least once.
 Loop while executes till it returns a zero value and until loop executes till it returns non-zero
value.
 Syntax:
until [ condition ]
do
commands
done
 Demo program for all the loops:

Output:

 To display Shell: echo $0


 To find version of the shell: echo “${BASH_VERSION}”
6 160120733057

 To find path: echo $PATH

PROGRAMS:

Program-1: Write the Shell script to find Factorial of a number.

Aim: To find the factorial of a number.

Description: In this program, we try to read a number from the user and find factorial for that
number

Program:
#!/bin/sh
echo "Enter a number: "
read n
a=1
product=1
while [ $a -le $n ]
do
product=$(( $product * $a ))
a=$(( $a + 1 ))
done
echo "The factorial of number $n is $product"

Output:

Conclusion:
By executing the above shell script, we have successfully found factorial for the number given by
user.

Program-2: Write the Shell Script to check if the given number is even or odd.

Aim: To check if the given number is even or odd.

Description: In this program, we try to read a number from the user and check whether the given
number is even or odd.

Program:
#!/bin/sh
echo "Enter a number: "
read n
7 160120733057

rem=$(( $n % 2 ))
if [ $rem -eq 0 ]
then
echo "$n is even"
else
echo "$n is odd"
fi

Output:

Conclusion:
By executing the above shell script, we have successfully checked whether the given number is even
or odd.

Program-3: Write a shell script to find power of a number.

Aim: To find power of a number.

Description: In this program, we try to read the base and power of a exponent and find the value of
that exponent.

Program:
#!/bin/sh
echo "Enter a number for base: "
read a
echo "Enter a number for power: "
read b
i=1
res=1
while [ $i -le $b ]
do
res=$(( $res * $a ))
i=$(( $i + 1 ))
done
echo "$a^$b = $res"

Output:

Conclusion:
By executing the above shell script, we have successfully found power of a number.
8 160120733057

Program-4: Write a Shell script to check whether the given number is palindrome or not.

Aim: To check whether the given number is palindrome or not.

Description: In this program, we try to read the number from the user and check whether the given
number is palindrome or not.

Program:
#!/bin/sh
echo "Enter a number: "
read n
temp=$n
re=0
while [ $n != 0 ]
do
r=$(( $n % 10 ))
re=$(( $re * 10 + $r ))
n=$(( $n / 10 ))
done
if [ $re -eq $temp ]
then
echo "The given number is a Palindrome"
else
echo "The given number is not a Palindrome"
fi

Output:

Conclusion:
By executing the above shell script, we have successfully checked whether the given number is
palindrome or not

Program-5: Write a Shell Script to print the Fibonacci Series.

Aim: To print the Fibonacci Series.

Description: In this program, we try to print the Fibonacci Series

Program:
#!/bin/sh
echo "Enter a number: "
read n
a=0
b=1
echo "Fibonacci Series: "
echo $a
echo $b
i=3
while [ $i -le $n ]
9 160120733057

do
c=$(( $a + $b ))
echo $c
a=$b
b=$c
i=$(( $i + 1 ))
done

Output:

Conclusion:
By executing the above shell script, we have successfully printed the Fibonacci Series

Program-6: Write a Shell Script to find whether a given number is prime or not.

Aim: To find whether a given number is prime or not

Description: In this program, we try to read a number from the user and check whether the given
number is prime or not

Program:
#!/bin/sh
echo "Enter a number: "
read n
flag=0
if [ $n -le 1 ]
then
flag=1
else
i=2
a=$(( $n / 2 ))
while [ $i -le $a ]
do
rem=$(( $n % $i ))
if [ $rem -eq 0 ]
then
flag=1;
break;
fi
i=$(( $i + 1 ))
done
fi
if [ $flag -eq 0 ]
then
echo "$n is a prime number"
10 160120733057

else
echo "$n is not a prime number."
Fi

Output:

Conclusion:
By executing the above shell script, we have successfully found whether the number given by the
user is prime or not

Program-7: Write a shell script to check if the given number is an Armstrong number or not.

Aim: To find whether given number is an Armstrong number or not.

Description: In this program, we try to read a number from the user and check whether the number
is an Armstrong number or not.

Program:
#!/bin/sh
echo "Enter a number: "
read n
res=0
num=0
temp=$n
while [ $temp -ne 0 ]
do
num=$(( $num + 1 ))
temp=$(( $temp / 10 ))
done
temp=$n
while [ $temp -ne 0 ]
do
r=$(( $temp % 10 ))
re=1
i=1
while [ $i -le $num ]
do
re=$(( $re * $r ))
i=$(( $i + 1 ))
done
res=$(( $res + $re ))
temp=$(( $temp / 10 ))
done
if [ $res -eq $n ]
then
echo "$n is an Armstrong number"
else
echo "$n is not an Armstrong number"
fi
11 160120733057

Output:

Conclusion:
By executing the above shell script, we have successfully checked whether the given number is
Armstrong number or not.

Program-8: Write a Shell Script to demonstrate Mini Calculator.

Aim: To demonstrate Mini Calculator

Description: In this program, we try to perform all basic arithmetic operations by demonstrating a
mini calculator.

Program:
#!/bin/sh
echo "Enter two numbers: "
read a
read b
echo "Operations to be Performed: "
echo "1. Add"
echo "2. Sub"
echo "3. Multiply"
echo "4. Division"
echo "Enter your Choice: "
read c
while [ $c -le 4 ]
do
case "$c" in
"1") sum=$(( $a + $b ))
echo "$a + $b = $sum"
;;
"2") diff=$(( $a - $b ))
echo "$a - $b = $diff"
;;
"3") mul=$(( $a * $b))
echo "$a * $b = $mul"
;;
"4") div=$(( $a / $b ))
echo "$a / $b = $div"
;;
esac
echo "Enter your choice: "
read c
done
12 160120733057

Output:

Conclusion:
By executing the above shell script, we have successfully demonstrated the Mini Calculator

You might also like