Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views3 pages

Lab Assignment 7 - Bennett

Bennett 2nd year lab assignment 7

Uploaded by

Aryan singh
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)
4 views3 pages

Lab Assignment 7 - Bennett

Bennett 2nd year lab assignment 7

Uploaded by

Aryan singh
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/ 3

School of Computer Science Engineering and Technology

Bennett University (The Times Group)

Program- BTech-3rd Semester Type- Sp. Core-I


Course Code- CSET213 Course Name-Linux and Shell Programming
Year- 2025 Semester- Odd
Date- 29/08/2025 Batch- Cyber Security (B15-28)

Lab Assignment 7

Exp No Name CLO Achieved Marks


CO1 CO CO3
2
7 Shell programming loops √ √ 2

Objective: To learn and use shell loops for the development of applications.
Outcomes: After hands-on you will be able to write basic shell scripts using loops for the
development of applications.

Hands-on Learning on loops (45 minutes)


1. For Loops: for loops iterate through a set of values until the list is exhausted
Example:

Method 1: Method 2: Method 3: Method 4:


for i in 1 2 3 4 5 for i in {1..5} echo "Bash version $ # set counter 'c' to 1 and
do do {BASH_VERSION}..." condition
echo " Iteration number echo "Iteration $i for i in {0..10..2} # c is less than or equal to 5
$i" times" do for (( c=1; c<=5; c++ ))
done done echo "Iteration $i times" do
done echo "Iteration $c times"
done

Conditional exit with break


for I in 1 2 3 4 5
do
statements1 #Executed for all values of ''I'', up to a disaster-condition if any.
statements2
if (disaster-condition)
then
break #Abandon the loop.
fi
statements3 #While good and, no disaster-condition.
done

Early continuation with continue statement


for I in 1 2 3 4 5
do
statements1 #Executed for all values of ''I'', up to a disaster-condition if any.
statements2
if (condition)
then
continue #Go to next iteration of I in the loop and skip statements3
fi
statements3
done
School of Computer Science Engineering and Technology
Bennett University (The Times Group)

Command substitution
for var in $(command)
do
print "$var"
done

2. while loop: The bash while loop is a control flow statement that allows code or commands to
be executed repeatedly based on a given condition.
while [ condition ] Example:
do Following while loop will print welcome 5 times on
command1 screen:
command2 #!/bin/bash
command3 x=1
done while [ $x -le 5 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done

3. until loop: The until loop is executed as many as times the condition/command evaluates to
false. The loop terminates when the condition/command becomes true.
Syntax:
until [ expression ]
do
code block
...
...
Done

Example: Print and count the number starting with 1 and increment it by 1. When the count is
equal to five, skip it. Similarly, the loop breaks when the count is equal to or greater than 10.

Code:
count=0
until false
do
((count++))
if [[ $count -eq 5 ]]
then
continue
elif [[ $count -ge 10 ]]
then
break
fi
echo "Counter = $count"
done

Hands-on on Arrays: Create an array that stores the first ten prime numbers. Iterate over the array
and print out each element inside it.
#!/bin/bash

prime=(2 3 5 7 11 13 17 19 23 29)
for i in "${prime[@]}"; do
School of Computer Science Engineering and Technology
Bennett University (The Times Group)
echo $i
done

Scripting Problems for Assessment (60 Minutes)


1. Write a Shell Bash Script for check if a provided number is
Armstrong or not. (15 Minutes) (Even Batch)
2. Write a Shell Script to check if a number entered through
keyboard is prime or not. Create two arrays named as prime and
notPrime. Print the arrays. (Even Batch)
3. Write a shell script that counts the number of lines, words,
and characters in two input files through command line
arguments and lists its size and permissions. (Odd Batch)
4. Write a script using for loop to display N asterisks (*), one
on each line, where N is a number given as command line
arguments. For example, if N is 5, your script should print
out: (Odd Batch)
*
* *
* * *
* * * *
* * * * *
Submission Instructions:
1. Submission requires the screen shots of all the incurred steps to execute a shell script or a video
showing the whole process.
2. All these files are in single zip folder.
3. Use the naming convention: Prog_CourseCode_RollNo_LabNo.docx (Example:
BCA3rdSem_CBCA221_ E21BCA002_Lab1.1)
4. Submission is through LMS only

You might also like