0% found this document useful (0 votes)
5 views7 pages

Lab 5

Uploaded by

mugheesahmad571
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)
5 views7 pages

Lab 5

Uploaded by

mugheesahmad571
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/ 7

Khawaja Fareed University of Engineering & Information

Technology
Rahim Yar Khan
Department Of Computer Engineering

Operating System Lab

Lab 04: Shell Scripting Practice


Lab Instructor: Ms. Aisha Naseer
Objective:
 Learn how to work with expressions in Shell Scripting
 Learn about conditional structures in Shell
 Learn about functions in Shell
 Hands on Practice on different shell scripts using case and functions.

Tips:

For example, the $ character represents the process ID number, or PID, of the current
shell.

echo $$

Figure showing process id of terminal.

The following table shows a number of special variables that you can use in your
shell scripts −

Sr.No Variable & Description


.

1
$0
The filename of the current script.
2
$n
These variables correspond to the arguments with which a script was invoked. Here n is a
positive decimal number corresponding to the position of an argument (the first argument is $1,
the second argument is $2, and so on).

3
$#
The number of arguments supplied to a script.

4
$*
All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1
$2.

5
$@
All the arguments are individually double quoted. If a script receives two arguments, $@ is
equivalent to $1 $2.

Example:

Lets suppose we have a file named: test.sh.

#!/bin/sh (writing this in kali(linux) is not necessary and


may generate an error!!)

echo "File Name: $0"


echo "First Parameter : $1"
echo "Second Parameter : $2"
echo "Quoted Values: $@"
echo "Quoted Values: $*"
echo "Total Number of Parameters : $#"

To run script:

Use bash or sh or chmod+x filename.sh to make it execute able.

We run it via:

bash test.sh
Result:

Case Statements:

Basic Syntax:
Example:

echo "Enter a number between 1 and 3:"


read number

case $number in
1)
echo "You selected option 1"
;; //Each case must end with double colon.
2)
echo "You selected option 2"
;;
3)
echo "You selected option 3"
;;
*) // Default case //
echo "Invalid option! Please choose a number between 1 and 3."
;;
esac // ending of case statement

Lab Activity 1.1:

Create a shell script that provides a basic menu for choosing a fruit.

The script should:

1. Display a menu with the following options:

1. Apple

2. Banana

3. Cake

4. Class

A proper message should be displayed to the user (1. Apple: “You


selected Apple”).
#!/bin/bash

# Display the menu

echo "Please choose an option from the menu:"

echo "1. Apple"

echo "2. Banana"

echo "3. Cake"

echo "4. Class"

# Read the user's choice

read -p "Enter your choice (1-4): " choice

# Use a case statement to respond to the user's choice

case $choice in

1)

echo "You chose Apple. A healthy choice!"

;;

2)

echo "You chose Banana. Great for energy!"

;;

3)

echo "You chose Cake. A delicious treat!"

;;

4)

echo "You chose Class. That's an unusual fruit!"

;;

*)

echo "Invalid choice. Please enter a number between 1 and 4."

;;

esac
Loops in Shell

 For Loop
 While Loop
 Until Loop
 Select Loop

1. For Loop:
The for loop is used to iterate over a list of items or a range of
numbers. It allows you to execute a block of commands multiple times.
There are two for loop in Shell
1) Iterating over a List 2) Iterating over a Range
Example:
Iterating over a List:
It is same as For Each Loop as in JAVA.

fruits=("Apple" "Banana" "Cherry") // Array of Strings

for fruit in "${fruits[@]}"; //[@] means that to access all


elements

do
echo "I like $fruit"
done // Completing loop

Iterating over a Range:


Example:

for ((i=1; i<=5; i++));


do
echo "Number: $i"
done

You might also like