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

Assignment No: 09

This document discusses conditional statements, looping statements, and case statements in Linux shell scripting. It provides examples of if/else, if/elif/else, nested if, switch statements. Looping statements like while and for are covered along with the break and continue commands. Practical examples are given to demonstrate if/else checking even/odd, profit/loss, prime numbers. Looping examples include menus, factorials, palindromes, number reversal. Case statements demonstrate reading integers and getting month names, finding days in a month, converting case. The conclusion is that control structures, switch statements, and looping were practiced.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Assignment No: 09

This document discusses conditional statements, looping statements, and case statements in Linux shell scripting. It provides examples of if/else, if/elif/else, nested if, switch statements. Looping statements like while and for are covered along with the break and continue commands. Practical examples are given to demonstrate if/else checking even/odd, profit/loss, prime numbers. Looping examples include menus, factorials, palindromes, number reversal. Case statements demonstrate reading integers and getting month names, finding days in a month, converting case. The conclusion is that control structures, switch statements, and looping were practiced.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Assignment No: 09

Aim: To study, implement and practice conditional and looping statements, case statements
in Linux.
Objective: To study and learn about the different conditional and looping statements, case
statements in Linux.
Theory:
Conditional Statements | Shell Script
Conditional Statements: There are total 5 conditional statements which can be used in bash
programming
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement

Their description with syntax is as follows:


if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks another
condition. In the syntax, if expression1 is false then it processes else part, and again expression2 will
be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
.
else
if [ expression2 ]
then
statement3
.
fi
fi
Switch statement
case statement works as a switch statement if specified value match with the pattern then it
will execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac

Looping Statements in Shell Scripting:


There are total 2 looping statements which can be used in bash programming
1. while statement
2. for statement
To alter the flow of loop statements, two commands are used they are,
1. break
2. continue
Their descriptions and syntax are as follows:
 while statemen
Here command is evaluated and based on the result loop will executed, if command
raise to false then loop will be terminated
Syntax:
while command
do
Statement to be executed
done
 for statement
The for loop operate on lists of items. It repeats a set of commands for every item in a
list.
Here var is the name of a variable and word1 to wordN are sequences of characters
separated by spaces (words). Each time the for loop executes, the value of the variable
var is set to the next word in the list of words, word1 to wordN.
Syntax:
for var in word1 word2 ...wordn
do
Statement to be executed
done

Practical:
Conditional statements in Linux:
1.) Write a shell program to check whether given number is even or odd.

Code:

Output:
2.) Write a shell script to determine the seller has profit or incurred loss and also determine
how much profit was made or loss may incurred if the cost price and selling price of the
product is entered through keyboard.

Code:

Output:

3.) Write a shell script to check whether the entered number is prime or not.
Code:
Output:

4.) Write a shell script to find the smallest and largest of three numbers.
Code:

Output:
5.) Write a shell program to find the gcd for the 2 given numbers.
Code:

Output:
Looping structures in Linux:

1.) Write a menu driven shell script which will print the following menu and find
a. Factorial of a number
b. Reverse of a number
c. Even / Odd
Code:

Output:
2.) Write a shell script to check entered string is palindrome or not.
Code:

Output:

3.) Write a shell program to reverse the digits of five digit integer.
Code:
Output:

4.) Write a shell script to display the digits which are in odd position in a given 5 digit
number.
Code:

Output:
Case statements in Linux:
1.) Write a shell script to read an integer and print the month of the year corresponding to that
number Give appropriate message if the number is not between 1 and 12.
Code:

Output:

2.) Write a shell script to find the number of days of the entered month.
Code:

Output:
3.) Write a shell Script to convert lowercase or uppercase as specified by user.
Code:

Output:
4.) Write a menu driven program to perform the following actions
a. Your current Directory
b. Today’s date
c. List of users logged in
Code:

Output:

Conclusion: From the above practical we learn the use of control statements ,switch
statements and looping.

You might also like