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

Lab 3 notes

Lab 3 notes provide detailed explanations of various Bash scripts, including their purposes, functionalities, and key concepts. The scripts cover tasks such as checking file existence, navigating directories, calculating factorials, and performing arithmetic operations. Each script is accompanied by comments explaining the flags and parameters used.

Uploaded by

amal98.alaskari
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)
5 views5 pages

Lab 3 notes

Lab 3 notes provide detailed explanations of various Bash scripts, including their purposes, functionalities, and key concepts. The scripts cover tasks such as checking file existence, navigating directories, calculating factorials, and performing arithmetic operations. Each script is accompanied by comments explaining the flags and parameters used.

Uploaded by

amal98.alaskari
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
You are on page 1/ 5

Lab 3 notes

Sure! Here's a breakdown of each script from Lab 03, with a detailed explanation of what it does
and how it works, including every flag, parameter, and concept used:

Task 1: Conditionals
1. exists.sh

Purpose: Checks if a file exists in the current directory.

#!/bin/bash
read -p "Enter filename: " fname
if [ -f "$fname" ]; then
echo "File '$fname' exists."
else
echo "File '$fname' does not exist."
fi

• -p flag in read -p: Prompts the user inline with a message.


• [ -f "$fname" ]: Tests if the file exists and is a regular file.

2. go.sh

Purpose: Navigates into a directory, shows file contents if it exists, or creates a new directory.

#!/bin/bash
read -p "Enter name: " name
if [ -d "$name" ]; then
cd "$name"
echo "Entered directory $name"
elif [ -f "$name" ]; then
cat "$name"
else
mkdir "$name"
echo "Directory $name created."
fi

• -d: True if the name is a directory.


• -f: True if the name is a file.
• cat: Prints contents of file.
• mkdir: Creates a new directory.
3. workday.sh

Purpose: Checks if a given day is a weekday or weekend.

#!/bin/bash
case $1 in
Sunday|Monday|Tuesday|Wednesday|Thursday)
echo "This is a workday";;
Friday|Saturday)
echo "This is a weekend";;
*)
echo "This is not a day";;
esac

• $1: First argument passed to the script.


• case ... esac: Matches input patterns.
• |: Used for multiple matching patterns.
• ;;: Ends each case pattern.

4. addtwo.sh

Purpose: Adds two numbers provided as arguments.

#!/bin/bash
sum=$(echo "$1 + $2" | bc)
echo "Sum: $sum"

• $1, $2: First and second arguments.


• bc: A calculator program used for real numbers.

5. max.sh

Purpose: Finds the maximum of three numbers.

#!/bin/bash
if [ $1 -ge $2 ] && [ $1 -ge $3 ]; then
echo "$1 is the largest."
elif [ $2 -ge $1 ] && [ $2 -ge $3 ]; then
echo "$2 is the largest."
else
echo "$3 is the largest."
fi

• -ge: Greater than or equal to.


• &&: Logical AND for combining conditions.
Task 2: Loops
1. factorial.sh

Purpose: Calculates the factorial of a number.

#!/bin/bash
fact=1
for (( i=1; i<=$1; i++ ))
do
fact=$((fact * i))
done
echo "Factorial: $fact"

• $1: Input number.


• for (( ... )): C-style loop.
• $((...)): Arithmetic expansion.

2. sumlist.sh

Purpose: Sums a list of numbers.

#!/bin/bash
sum=0
for num in "$@"
do
sum=$((sum + num))
done
echo "The total sum is: $sum"

• $@: All arguments.


• "$@": Preserves each argument separately.

3. guess.sh

Purpose: Number guessing game.

#!/bin/bash
secret=$(( (RANDOM % 100) + 1 ))
attempts=0

while true
do
read -p "Enter your guess: " guess
((attempts++))
if [ "$guess" -lt "$secret" ]; then
echo "Too Low!"
elif [ "$guess" -gt "$secret" ]; then
echo "Too High!"
else
echo "Correct! You guessed in $attempts attempts."
break
fi
done

• $RANDOM: Generates a random number.


• % 100 + 1: Ensures the range is 1 to 100.
• ((...)): Used for integer arithmetic.
• while true: Infinite loop.
• break: Exits the loop.

Task 3: Functions
multiops.sh

Purpose: Script with multiple operations using functions.

#!/bin/bash

add() {
echo "Sum: $(($1 + $2))"
}

sum() {
total=0
for n in "$@"; do
total=$((total + n))
done
echo "Sum of list: $total"
}

fac() {
f=1
for (( i=1; i<=$1; i++ )); do
f=$((f * i))
done
echo "Factorial: $f"
}

if [ "$1" = "-a" ]; then


add "$2" "$3"
elif [ "$1" = "-s" ]; then
shift
sum "$@"
elif [ "$1" = "-f" ]; then
fac "$2"
else
echo "Usage: $0 -a num1 num2 | -s numlist | -f number"
fi

• add(), sum(), fac(): Function definitions.


• shift: Moves arguments left (drops $1).
• $0: Script name.

Let me know if you'd like each script exported separately into files or zipped for submission!

You might also like