0% found this document useful (0 votes)
89 views9 pages

Eecs 2032 - Labtest1-Prep

Uploaded by

nurjahanahmed24
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)
89 views9 pages

Eecs 2032 - Labtest1-Prep

Uploaded by

nurjahanahmed24
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/ 9

LabTest Practice 01

Q1: Read two integers from the standard input and display on the same line the integers
between them not counting the two integers you read.
For example if you read 3 12, you display
4 5 6 7 8 9 10 11

#!/bin/bash

read -p "2 int: " a b


a=$((a+1))

while [ "$a" -lt "$b" ]; do


echo -n "$a "
a=$((a+1))
done

echo

Q2: Read 3 integers and display the median number

#!/bin/bash
read -p "enter 3 numbers: " a b c

#b<a<c \\ c<a<b == a

if [ "$a" -lt "$c" -a "$a" -gt "$b" ] || [ "$a" -lt "$b" -a "$a" -gt "$c" ]; then
echo $a

#a<b<c \\ c<b<a

elif [ "$b" -lt "$c" -a "$b" -gt "$a" ] || [ "$b" -lt "$a" -a "$b" -gt "$c" ]; then
echo $b

#a<c<b \\ b<c<a

elif [ "$c" -lt "$b" -a "$c" -gt "$a" ] || [ "$c" -lt "$a" -a "$c" -gt "$b" ]; then
echo $c

else
echo "Error"

fi
Q3: Write a bash script that reads from a text file (p2a.txt) Each line in the file contains either
4 or 5 integers.
Display the line where the first integer is greater than the sum of the second and third
integers.

#!/bin/bash

while read -r a b c d e; do
sum=$((b+c))
if [ "$a" -gt "$sum" ]; then
echo $a $b $c $d $e
fi
done < p2a.txt

Q4: Write a bash script that takes one command line augment (a number represents the
number of units of a product).
Then it checks a file inv.txt
example:
bash labtest1A_4.sh 45
It displays the product number and product name of any product with inventory 44, 45, or 46

#!/bin/bash
IFS=$'\t'
myinv=$1

while read -r id name price inv; do


if [ "$myinv" == "$inv" -o "$myinv" == "$((inv-1))" -o "$myinv" == "$((inv+1))" ]; then
echo $name $id
fi
done < inv.txt
~

Q5: Write a sed script that displays only lines that contains at least two non neighboring digits

/[0-9][^0-9]\+[0-9]/!d
// any digit 0-9 > non-digit character > \+ one or more occurrences > 0-9 > don’t delete

LabTest Practice 02
Q1: Write a bash script that reads a directory name. First it checks if that directory is in the
current directory, if yes it displays the number of files in that directory, otherwise it
displays “No such directory”

#!/bin/bash

read -p "enter a directory: " dir

if [ -d "$dir" ]; then
files=$(find "$dir" -type f | wc -l)
echo "In directory $dir, there are $files files"
else
echo "No such directory"
fi

Q2: Write a bash script that takes a variable number of integers as command line arguments. if
these arguments are ascending it displays “Ascending” if not, displays “NO”

#!/bin/bash

if [ $# -lt 2 ]; then
echo "enter more than 2 numbers."
exit 1
fi

prev=$1

for i in "${@:2}"; do
if [ "$prev" -gt "$i" ]; then
echo "NO"
exit 1
fi

prev=$i
done
echo "ASCENDING”

Q3: Write a bash script that reads a student name and displays a message stating if the
student has paid the tuition or not.

#!/bin/bash

read -p "enter last,first name: " name


record=$(grep -P "^${name}\t" A.txt)
sid=$(echo "$record" | cut -f2)
tuition=$(grep -P "^${sid}\t" B.txt)

st=$(echo "$tuition" | cut -f2)

if [ "$st" -eq 1 ]; then


echo "$name paid"
else
echo "$name didn't pay"
fi

Q4: Write a shell script that reads integers each integer is on a line. It adds all the integers
until it reads a negative number. After reading a negative number, it stops adding and
display the total sum.

#!/bin/bash

sum=0

while true; do

read -p "enter an integer: " a

// On this condition, end the if


if [ "$a" -lt 0 ]; then
break
fi

sum=$((sum+a))

done

echo $sum

Q5: Check arithmetic, geometric or neither sequence


#!/bin/bash
read -p "enter 3 numbers: " a b c

if [ $((b-a)) -eq $((c-b)) ]; then


echo "Arith"
elif [ "$a" -ne 0 -a "$b" -ne 0 ]; then
r1=$(echo "scale=2; (($b / $a))" | bc)
r2=$(echo "scale=2; (($c / $b))" | bc)
if [ "$r1" == "$r2" ]; then
echo "geo"
else
echo "none"
fi
else
echo "none"
fi

Notes:
→ == / = : these are used for String, NOT NUMBERS
→ -eq : compare numbers

Lab 2

Q1: Good day, Mr/Ms xxx! today is yyy.

#!/bin/bash
echo "good day, mr/ms $USER! Today is $(date +%A)."

Q2: $ bash lab2 2.sh kindness is my number one attribute in a human being!
kindness - number one attribute in a human being!

#!/bin/bash
echo $1 - “${@:4}”

Q3: $ bash lab2 3.sh 3 4 5 7


Please enter an integer: 4
The argument at position 4 is: 7.

$ bash lab2 3.sh 1 7 6


Please enter an integer: 8
No argument has been provided at position 8.

$ bash lab2 3.sh


No arguments provided.

#!/bin/bash
if [ $# -le 0 ]; then
echo "nothing"
exit 1
fi

read -p "int?: " n

if [ "$n" -le "$#" ]; then


echo "value at $n is ${!n}"
else
echo "out of bound"
fi

Lab 3

Q1: Read two numbers and determine the relation.

#!/bin/bash

read -p "Enter two numbers: " a b

if [ -z "$a" -o -z "$b" ]; then


echo "Please enter at least two numbers."
exit 1
fi

if [ "$a" -eq "$b" ]; then


echo "These two numbers are equal."
exit 1
fi

if [ "$a" -gt "$b" ]; then


big=$a
small=$b
else
big=$b
small=$a
fi

if [ $((big % small)) -eq 0 ]; then


n=$((big/small))
echo "$big is $n times bigger than $small"
else
echo "No relation!"
fi

Q2: Play a guessing game with two functions: generate_random_number and play_game.

#!/bin/bash

generate_random_number(){
echo $(( (RANDOM % 100) + 1)) // generate random number
}

play_game(){
// declare the variables
local target=$(generate_random_number)
local attempts=0
local guess=""

while true; do

read -p "enter your guess: " guess

// handle the exit case


if [ "$guess" == "exit" ]; then
echo "Thanks for playing. Target was $target."
exit 1
fi

// handle non-integer number


if [[ ! "$guess" =~ ^[0-9]+$ ]]; then
echo "enter integer"
continue
fi

// update variables
guess=$(($guess))
attempts=$((attempts+1))

// compare
if [ "$guess" -lt "$target" ]; then
echo "Guess a higher number."
elif [ "$guess" -gt "$target" ]; then
echo "Guess a lower number."
else
echo "Congrats. You guessed it in $attempts try."
break
fi
done

Q3: Read 2 different .txt files (books.txt & borrowed.txt) where


Books.txt: id title author available_copies
Borrowed.txt: id borrowed copies
If available copies are less than borrowed copies, echo the book title
IFS is colon (:) here

#!/bin/bash

// step1: read the id and borrowed copies from borrowed.txt file


while IFS=: read -r id borrowed_copies; do

// step2: differentiate the asked id line from books.txt and cut the book_title and available copies
record=$(grep -P "^$id:" books.txt)

if [[ ! -z "$record" ]]; then


title=$(echo "$record" | cut -d: -f2)
available_copies=$(echo "$record" | cut -d: -f4)

// step3: compare and echo the result


if [[ "$available_copies" -lt "borrowed_copies" ]]; then
echo $title
fi
fi

done < borrowed.txt

Q4: Delete any line that doesn’t have ABABAB & replace rain with heavy rain

/ABABAB/!d // don’t delete any line with ABABAB


/rain/s/rain/heavy rain/ // find rain and change rain to heavy rain

Notes:
→ /d : Delete Matching Lines
→ /!d: Delete non-matching lines

Lab 4

Q1: a. (123)456-7890 → 123-456-7890

s/(\([0-9]\{3\}\))\([0-9]\{3\}-[0-9]\{4\}\)/\1-\2/

a. 123-456-7890 → (123) 456-7890

s/\([0-9]\{3\}\)-\([0-9]\{3\}\)-\([0-9]\{4\}\)/(\1)\2-\3/

→ run in terminal : sed -f sedfile.sed sedfile.txt

You might also like