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

Lab 3

The document outlines a series of lab tasks performed by Syed Anfas, including scripts for directory cleanup, book management, and system management. Each task involves creating and managing files and directories, as well as user accounts through Bash scripts. The scripts include functionalities for removing old files, adding book details, and listing files in specified directories.

Uploaded by

opfarasat
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)
4 views13 pages

Lab 3

The document outlines a series of lab tasks performed by Syed Anfas, including scripts for directory cleanup, book management, and system management. Each task involves creating and managing files and directories, as well as user accounts through Bash scripts. The scripts include functionalities for removing old files, adding book details, and listing files in specified directories.

Uploaded by

opfarasat
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/ 13

23k-0574 SYED ANFAS

OS LAB 3

IN LAB TASKS:

TASK 1:

TASK 2:
23k-0574 SYED ANFAS
23k-0574 SYED ANFAS

POST LAB TASKS:

TASK 1:
23k-0574 SYED ANFAS

TASK 2:
23k-0574 SYED ANFAS

TASK 3:
23k-0574 SYED ANFAS
23k-0574 SYED ANFAS

TASK 4:

TASK 5:

#!/bin/bash

# Function to display usage


usage() {
echo "Usage: $0 <directory_path> <days>"
echo " directory_path: Path to the directory to clean up"
echo " days: Number of days to consider for removing old files"
exit 1
}

# Check if both arguments are provided


if [ $# -ne 2 ]; then
usage
fi

# Get the directory path and days from the arguments


dir_path="$1"
23k-0574 SYED ANFAS
days="$2"

# Check if the provided directory exists


if [ ! -d "$dir_path" ]; then
echo "The directory $dir_path does not exist."
exit 1
fi

# Find and remove files older than the specified number of days
echo "Removing files older than $days days in $dir_path..."
find "$dir_path" -type f -mtime +$days -exec rm -f {} \;
echo "Files older than $days days have been removed."

# Recursively remove empty directories


echo "Removing empty directories in $dir_path..."
find "$dir_path" -type d -empty -exec rmdir {} \;
echo "Empty directories have been removed."

echo "Directory cleanup completed."


23k-0574 SYED ANFAS

TASK 6:
#!/bin/bash

# Function to create a directory structure and write book details


create_book_directory() {
# Define the base directory path (Books directory on the Desktop)
base_dir="$HOME/Desktop/Books"

# Check if the directory already exists


if [ -d "$base_dir" ]; then
echo "The 'Books' directory already exists on the Desktop."
else
# Create the 'Books' directory
mkdir -p "$base_dir"
echo "'Books' directory created on Desktop."
fi

# Ask for book details


echo "Enter Book Name: "
read book_name
echo "Enter Author: "
read author
echo "Enter Price: "
read price
echo "Enter Publication: "
read publication
echo "Enter Stock Quantity: "
read stock

# Create a file for the book with book name as the file name
book_file="$base_dir/$book_name.txt"

# Write book details to the file


echo "Book Name: $book_name" > "$book_file"
echo "Author: $author" >> "$book_file"
echo "Price: $price" >> "$book_file"
echo "Publication: $publication" >> "$book_file"
echo "Stock Quantity: $stock" >> "$book_file"

echo "Details for '$book_name' have been saved to $book_file."


}

# Function to list all books (files) in the "Books" directory


list_books() {
base_dir="$HOME/Desktop/Books"

# Check if the "Books" directory exists


if [ -d "$base_dir" ]; then
# List all files (books) in the directory
echo "Listing all books in 'Books' directory:"
ls "$base_dir"/*.txt
23k-0574 SYED ANFAS
else
echo "The 'Books' directory does not exist on the Desktop."
fi
}

# Main Menu: Allow user to choose functionality


echo "Book Manager"
echo "1. Create Book Directory and Add Book Details"
echo "2. List All Books"
echo "3. Exit"
read -p "Choose an option (1/2/3): " choice

case $choice in
1)
create_book_directory
;;
2)
list_books
;;
3)
echo "Exiting the Book Manager."
exit 0
;;
*)
echo "Invalid option. Please try again."
exit 1
;;
esac
23k-0574 SYED ANFAS

TASK 7:

#!/bin/bash

# Function 1: Create a user account and set a password


create_user_account() {
echo "Enter the username to create: "
read username

# Check if the user already exists


if id "$username" &>/dev/null; then
echo "User $username already exists."
return
fi

# Create the user account


sudo useradd "$username"
if [ $? -eq 0 ]; then
echo "User $username created successfully."
else
echo "Failed to create user $username."
return
fi

# Set the password for the user


echo "Enter password for user $username: "
sudo passwd "$username"
if [ $? -eq 0 ]; then
echo "Password for $username set successfully."
else
echo "Failed to set password for $username."
fi
}

# Function 2: Create directories and subdirectories


create_directories() {
echo "Enter the directory path to create: "
23k-0574 SYED ANFAS
read dir_path

# Create the main directory and any subdirectories


mkdir -p "$dir_path"
if [ $? -eq 0 ]; then
echo "Directory structure created at $dir_path."
else
echo "Failed to create the directory structure."
fi
}

# Function 3: List all hidden and unhidden files in the specified directory
list_files() {
if [ $# -ne 1 ]; then
echo "Usage: $0 <directory_path>"
return 1
fi

directory="$1"

# Check if the directory exists


if [ ! -d "$directory" ]; then
echo "The directory $directory does not exist."
return 1
fi

echo "Listing all files (including hidden and unhidden) in $directory:"

# List all files, including hidden files (those starting with a dot)
ls -la "$directory"
}

# Main menu to allow user to choose functionality


echo "System Management Script"
echo "1. Create User Account and Set Password"
echo "2. Create Directories and Subdirectories"
echo "3. List All Files in a Directory"
echo "4. Exit"
read -p "Choose an option (1/2/3/4): " choice

case $choice in
1)
create_user_account
;;
2)
create_directories
;;
3)
read -p "Enter the directory path to list files: " dir_path
list_files "$dir_path"
;;
4)
echo "Exiting the script."
exit 0
23k-0574 SYED ANFAS
;;
*)
echo "Invalid option. Please try again."
exit 1
;;
esac

You might also like