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

Lab 3 OSG

The document contains code for a Bash script that defines several functions to verify different aspects of a system including the processor, kernel version, installed software, OS version, memory, serial number, and IP address. The script then provides an interactive menu to call the different verification functions.

Uploaded by

dangphhe172554
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)
22 views9 pages

Lab 3 OSG

The document contains code for a Bash script that defines several functions to verify different aspects of a system including the processor, kernel version, installed software, OS version, memory, serial number, and IP address. The script then provides an interactive menu to call the different verification functions.

Uploaded by

dangphhe172554
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

Code :

#!/bin/bash

# Function to calculate directory size

get_directory_size() {

local dir_path=$1

local total_size=0

# Calculate size of files and subdirectories recursively

for item in "$dir_path"/*; do

if [ -f "$item" ]; then

# Add file size to total

local file_size=$(du -b "$item" | awk '{print $1}')

total_size=$((total_size + file_size))

elif [ -d "$item" ]; then

# Recursively calculate subdirectory size

local sub_dir_size=$(get_directory_size "$item")

total_size=$((total_size + sub_dir_size))

fi

done

# Return the total size of the directory

echo "$total_size"

# Prompt for directory path

read -p "Enter directory path: " directory


# Check if directory exists

if [ -d "$directory" ]; then

# Call the function to get the directory size

directory_size=$(get_directory_size "$directory")

echo "The size of $directory is $directory_size bytes."

else

echo "Directory not found."

Fi

Bài 2:

Code :

#!/bin/bash

# Check if file/directory path is provided

if [ -z "$1" ]; then

echo "Usage: $0 <path>"

exit 1

fi

# Check if file/directory exists

if [ ! -e "$1" ]; then

echo "File or directory '$1' does not exist."

exit 1

fi

# Check if it's a regular file


if [ -f "$1" ]; then

echo "'$1' is a regular file."

elif [ -d "$1" ]; then

echo "'$1' is a directory."

else

echo "'$1' exists but is neither a file nor a directory."

fi

# Check if file/directory is readable

if [ -r "$1" ]; then

echo "'$1' is readable."

else

echo "'$1' is not readable."

fi

# Check if file/directory is writable

if [ -w "$1" ]; then

echo "'$1' is writable."

else

echo "'$1' is not writable."

fi

# Check if file/directory is executable

if [ -x "$1" ]; then

echo "'$1' is executable."

else

echo "'$1' is not executable."

fi
# Check file/directory permissions

permissions=$(ls -ld "$1" | awk '{print $1}')

echo "Permissions of '$1': $permissions"

bài 3
#!/bin/bash

# Function to display desktop processor information

function verify_desktop_processor() {

echo "=== Desktop Processor Verification ==="

echo "Processor Model: $(cat /proc/cpuinfo | grep 'model name' | head -n 1 | awk -F ': ' '{print $2}')"

echo

# Function to display system kernel version

function verify_system_kernel() {

echo "=== System Kernel Verification ==="

echo "Kernel Version: $(uname -r)"

echo

}
# Function to display installed software

function verify_installed_software() {

echo "=== Installed Software Verification ==="

echo "Installed Software:"

dpkg-query -l

echo

# Function to display OS version

function verify_os_version() {

echo "=== OS Version Verification ==="

echo "Operating System Version: $(lsb_release -ds)"

echo

# Function to display desktop memory information

function verify_desktop_memory() {

echo "=== Desktop Memory Verification ==="

echo "Total Memory: $(free -h | awk '/^Mem/ {print $2}')"

echo "Used Memory: $(free -h | awk '/^Mem/ {print $3}')"

echo "Available Memory: $(free -h | awk '/^Mem/ {print $7}')"

echo

# Function to display system serial number

function verify_serial_number() {

echo "=== System Serial Number Verification ==="

echo "Serial Number: $(dmidecode -t system | grep "Serial Number" | awk -F ': ' '{print $2}')"
echo

# Function to display system IP address

function verify_system_ip() {

echo "=== System IP Verification ==="

echo "IP Address: $(hostname -I)"

echo

# Main script

while true; do

echo "Choose an option below!"

echo

echo "1 - Verify desktop processor"

echo "2 - System Kernel Verification"

echo "3 - Verify installed software"

echo "4 - OS version"

echo "5 - Verify desktop memory"

echo "6 - Verify the serial number"

echo "7 - Verify System IP"

echo "8 - Exit"

echo

read -p "Selected option: " option

echo

case "$option" in

1)
verify_desktop_processor

;;

2)

verify_system_kernel

;;

3)

verify_installed_software

;;

4)

verify_os_version

;;

5)

verify_desktop_memory

;;

6)

verify_serial_number

;;

7)

verify_system_ip

;;

8)

echo "Exiting..."

exit 0

;;

*)

echo "Invalid option: $option"

echo

;;

esac
done

You might also like