You are on page 1of 4

Academic Year: 2022-23 Name of Student: Imran

Semester: IV Student ID:22104049


Class / Branch: SE- IT Roll No.62
Subject: UNIX Lab Date of Submission:26/03/24
Name of Instructor: Sonal Jain

Experiment No.:8
Aim: Execution of advanced programs of shell scripting.
a. Write a shell script to implement menu-driven calculator using case statement.
b. Write a shell script to compare two strings.
c. Write a shell script to read and check if the directory / file exists or not, if not make
the directory / file.
Output:
a. Write a shell script to implement menu-driven calculator using case statement.

The basic syntax of the case...esac statement is to give an expression to


evaluate and to execute several different statements based on the value
of the expression.
The interpreter checks each case against the value of the expression until
a match is found. If nothing matches, a default condition will be used.
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
*)
Default condition to be executed
;;
esac

Department of Information Technology | APSIT


2. Write a shell script to compare two strings

When writing Bash scripts you will often need to compare two strings to check if they are
equal or not. Two strings are equal when they have the same length and contain the same
sequence of characters.

Department of Information Technology | APSIT


Comparison operators are operators that compare values and return true or false. When
comparing strings in Bash you can use the following operators:

 string1 = string2 and string1 == string2 - The equality operator returns true if the
operands are equal.
 Use the = operator with the test [ command.
 Use the == operator with the [[ command for pattern matching.
 string1 != string2 - The inequality operator returns true if the operands are not equal.
 string1 =~ regex- The regex operator returns true if the left operand matches the
extended regular expression on the right.
 string1 > string2 - The greater than operator returns true if the left operand is greater
than the right sorted by lexicographical (alphabetical) order.
 string1 < string2 - The less than operator returns true if the right operand is greater
than the right sorted by lexicographical (alphabetical) order.
 -z string - True if the string length is zero.
 -n string - True if the string length is non-zero.

3. When working with Bash and shell scripting, you might need to check whether a
directory or a file exists or not on your filesystem.
Based on this condition, you can exit the script or display a warning message for the end user
for example.

In order to check whether a file or a directory exists with Bash, you are going to use “Bash
tests”.

Department of Information Technology | APSIT


In this tutorial, you are going to learn how to check if a file or directory exists in a Bash
script.

Conclusion: In this experiment we have executed advanced programs of Shell Scripting

Department of Information Technology | APSIT

You might also like