0% found this document useful (0 votes)
9 views

[OS] LAB 3 template

Uploaded by

khoinguyenpkn
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)
9 views

[OS] LAB 3 template

Uploaded by

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

Name: Phạm Khôi Nguyên

Student ID: 23521055


Class: CTTT2023

OS
LAB 3

CHECKLIST
1. Homework
1 2 3 4 5 6
Explanation
Your code
Results and discussions

Mark: 9.5

1
2. Lab 3
1. Given the file gradebook.csv which contain grades of a class. Research how to use
IFS to read csv file. Use shell script to:
• Read Student ID from command line
• Use Student ID to find and print name, original grade, and letter-based grade
of corresponding student
Answer:

2
3
get_student_grade.sh:
#!/bin/bash
#Check if the provided student ID is empty or not. If empty, exit and return 1.

4
if [ -z "$1" ]; then
echo "Please provide a Student ID as an argument."
exit 1
fi
student_id="$1"
found=0
#Split the lines of input into separate words or fields
IFS=';'
#Variable for each respected columns
while read -r id name original_grade letter_grade; do
if [ "$id" == "$student_id" ]; then
#Trim every whitespace from original_grade
original_grade=$(echo "$original_grade" | tr -d '[:space:]')
#[900, 100]
if [ "$original_grade" -ge 900 ] && [ "$original_grade" -le 1000 ]; then
letter_grade="A+"
#[800, 900)
elif [ "$original_grade" -ge 800 ] && [ "$original_grade" -lt 900 ]; then
letter_grade="A"
#[700, 800)
elif [ "$original_grade" -ge 700 ] && [ "$original_grade" -lt 800 ]; then
letter_grade="B+"
#[600, 700)
elif [ "$original_grade" -ge 600 ] && [ "$original_grade" -lt 700 ]; then
letter_grade="B"
#[500, 600)
elif [ "$original_grade" -ge 500 ] && [ "$original_grade" -lt 600 ]; then
letter_grade="C"
# < 500
else
letter_grade="D/F"
fi
echo "Name: $name"
echo "Original Grade: $original_grade"
echo "Letter-based Grade: $letter_grade"
found=1
break
fi
#Read from second row, avoid header
done < <(tail -n +2 ~/gradebook.csv)
if [ "$found" -eq 0 ]; then
echo "Student ID not found in gradebook."
fi

5
2. Write a shell script to let user input their age, then:
• Check if input is a number and its value is between 0 and 100.
• If input is less than 12  Print “You are a child”
• If input is from 12 – 18  Print “You are a teenager”
• If input is greater than 18  Print “You are an adult”
Answer:

6
check_age.sh:
#!/bin/bash
# Check if the input is valid
if [ -z "$1" ]; then
echo "Please input your age."
exit 1
fi
age="$1"
# Check if the age is [0, 100]
if [ "$age" -ge 0 ] && [ "$age" -le 100 ]; then
# Check if the age is [0, 12)
if [ "$age" -lt 12 ]; then
echo "You are a child"
# Check if the age is [12, 18]
elif [ "$age" -ge 12 ] && [ "$age" -le 18 ]; then
echo "You are a teenager"
# Check if the age is [19, 100]
else
echo "You are an adult"
fi
else
echo "Please enter a number between 0 and 100."

7
fi
3. Write a program that read 02 arguments from command line: first argument is a
string while second argument is a path.
Check if that string exists in any text file (for example, test.txt) in the provided path.
Print the line containing the string.

Answer:

8
find_string.sh:
#!/bin/bash
#If the number of arguments is less than 2, return 1
if [ $# -ne 2 ]; then
#Correct format if the number of arguments is not valid
echo "Usage: $0 <string> <path>"
exit 1
fi
search_string="$1"
directory_path="$2"
#Check if the provided path is a valid directory
if [ ! -d "$directory_path" ]; then
echo "The provided path is not a valid directory."
exit 1
fi
#Use 'grep' to search for the string in all .txt files in the provided directory
#-H shows the filename, -n shows the line number
grep -Hn "$search_string" "$directory_path"/*.txt

You might also like