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

Shell Scripting Lab Tasks Guide

The document outlines graded lab tasks for students to write shell scripts, focusing on creating directories, copying files, searching strings, and merging files. It also explains the use of conditional and looping statements in shell scripting, providing syntax examples and practical activities. The objective is to enhance students' skills in writing effective shell scripts using various programming constructs.

Uploaded by

Amna
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)
10 views7 pages

Shell Scripting Lab Tasks Guide

The document outlines graded lab tasks for students to write shell scripts, focusing on creating directories, copying files, searching strings, and merging files. It also explains the use of conditional and looping statements in shell scripting, providing syntax examples and practical activities. The objective is to enhance students' skills in writing effective shell scripts using various programming constructs.

Uploaded by

Amna
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

3) Graded Lab Tasks

Note: The instructor can design graded lab activities according to the level of difficult and complexity
of the solved lab activities. The lab tasks assigned by the instructor should be evaluated in the same lab.

Task 1:
Write a shell script that asks the user to enter the name of the directory and the destination; and then
creates a directory at the given destination. Also display a success message along with the list of
directories only.

Task 2:
In this task you have to re-write the cp command in such a way that your script asks the users to enter the
path of files to be copied and the destination; and then copies all of the files at the destination. Also
display a success message along with the list of directories only.

Task 3:
Write a shell script that takes a file path and a search string as input; and displays all of the lines that
contain that string.

Task 4:
Write a shell script that takes a list of text files as input and merges all those files into a single file; and
displays its contents in the less application.

112
Lab No. 12
Writing Shell Scripts using Conditional-Statements, and
Loops
Objective
The objective of this lab is to familiarize the students with the use of conditional and looping statements
in shell scripting.

Activity Outcomes:
On completion of this lab students will be able to
 Write shell scripts that uses conditional statements
 Use looping statements available in bash shell

Instructor Notes
As pre-lab activity, read Chapter 27 &31 from the book “The Linux Command Line”, William E. Shotts,
Jr.
1) Useful Concepts
Conditional Statements
A conditional statement tells a program to execute an action depending on whether a condition is true or
false. It is often represented as an if-then or if-then-else statement.

if Statement
if statement is provided in many variant in bash shell. The most commonly used syntax is as given below:

if [ condition ]

then

commands

fi
Note that there must be a space between condition and both opening and closing brackets. The syntax of
else-if command is

if [ condition ]

then

commands

elif [ condition ]

113
then

commands

else

commands

fi
The following example shows the use of if statement. In this example, we take a number as input from
user and check whether it is even or odd.
#! /bin/bash
echo “Please Enter a Number”
read num
if [ $((num%2)) –eq 0 ]
then
echo “$num is an even
number”
else
echo “$num is an odd
number”
fi

Exit Status
Commands issue a value to the system when they terminate, called an exit status. This value, which is an
integer in the range of 0 to 255, indicates the success or failure of the command’s execution. By
convention, a value of zero indicates success and any other value indicates failure. The exit status of a
command is saved in a system variable $?.

Other syntax for if statement


Recent versions of bash include a compound command that acts as an enhanced replacement for test. It
uses the following syntax:
if [[ condition ]]
then
commands
fi

Similarly, (( )) syntax can also be used which is designed for arithmetic expressions.

114
Case statement
The bash case statement is generally used to simplify complex conditionals when you have multiple
different choices. Using the case statement instead of nested if statements will help you make your bash
scripts more readable and easier to maintain. The syntax of case statement is:

case EXPRESSION
in
Pattern-1)

Commands
;;
Pattern-2)

Commands
;;
.
.
.
Pattern-n)

Commands
;;
*)

Commands
;;
esac

Following are some examples of some valid patterns for the case statement.
Pattern Description
a) Matches if word equals “a”.
[[ :alpha: ]] Matches if word is a single alphabetic character.
???) Matches if word is exactly three characters long
*.txt) Matches if word ends with the characters “.txt”.
*) Matches any value of word. It is good practice to include this as the last pattern in a
case command, to catch any values of word that did not match a previous pattern; that
is, to catch any possible invalid values.

Looping Statements
A program loop is a series of statements that executes for a specified number of repetitions or until
specified conditions are met. While, until and for are the common looping statement provided by bash
shell.
For Loop
The original syntax of for loop is:
for variable in
values
do
statements
115
done

Following example shows the working of for loop


#!/bin/bash Out-Put
for i in A B C A
D E B
do C
echo $i D
done
E

We can give a range of values as {0..9}. bash shell also supports a C like syntax of for loop which is
given below:
for (( Expression; Expression2; Expression))
do
statements
done

While Loop
The syntax of while loop is as given below
While (( Condition ))
do
statements
done

Following example shows the working of while loop


#!/bin/bash Out-Put
count=1 1
while [[ $count -le 5 ]] 2
do 3
echo $count 4
count=$((count + 1))
5
done

Breaking the while loop: bash provides two built-in commands that can be used to control program flow
inside loops. The break command immediately terminates a loop, and program control resumes with the
next statement following the loop. The continue command causes the remainder of the loop to be skipped,
and program control resumes with the next iteration of the loop.

Until Loop
The until command is much like while, except instead of exiting a loop when a nonzero exit status is
encountered, it does the opposite. An until loop continues until it receives a zero exit status i.e. the
condition becomes true.
#!/bin/bash Out-Put
count=1 1
until [[ $count -gt 5 2
]] 3
do 4
echo $count
116
count=$((count + 1)) 5
done
2) Solved Lab Activities
[Link] Allocated Time Level of Complexity CLO Mapping
1 10 Low CLO-6
2 15 Medium CLO-6
3 15 High CLO-6

Activity 1:
Write a shell script that modifies the mkdir command as: first it takes the directory name from the user
as input and checks if a directory with same name exists then shows an error message otherwise
creates the directory and show the success message.
Solution:
Code

Out-put

Activity 2:
Write a shell script that takes a word as input and finds whether it is a single alphabet, ABC followed
by a digit, of length 3, ends with .txt or it is something else.
Solution:
Code Out-put
#!/bin/bash Out-Put
read -p "enter word > " 1
case $REPLY in 2
[[:alpha:]]) echo "is a single alphabetic character." ;; 3
117
[ABC][0-9]) echo "is A, B, or C followed by a digit." ;; 4
???) echo "is three characters long." ;; 5
*.txt) echo "is a word ending in '.txt'" ;;
*) echo "is something else." ;;
esac

Activity 3:
Write a shell script that, given a filename as the argument will write the even numbered line to a file
with name even file and odd numbered lines in a text file called odd file.
Solution:
Code

Output

118

You might also like