0% found this document useful (0 votes)
16 views6 pages

Lab 02 Os

The document contains a series of Bash scripts for various tasks including summing two numbers, counting words in a sentence, appending the current date to a file, checking if a name corresponds to a file or directory, and cleaning up a directory by removing old files and empty directories. Each script includes user input prompts and error handling for invalid inputs. The scripts demonstrate basic Bash programming concepts and file management operations.

Uploaded by

5pdhqvhxfp
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)
16 views6 pages

Lab 02 Os

The document contains a series of Bash scripts for various tasks including summing two numbers, counting words in a sentence, appending the current date to a file, checking if a name corresponds to a file or directory, and cleaning up a directory by removing old files and empty directories. Each script includes user input prompts and error handling for invalid inputs. The scripts demonstrate basic Bash programming concepts and file management operations.

Uploaded by

5pdhqvhxfp
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/ 6

LAB 03 OS

K224126-WISHMA SHAHAB

Q1.
#!/bin/bash

if [ $# -ne 2 ]; then
echo "Usage: $0 num1 num2"
exit 1
fi

num1=$1
num2=$2
sum=$((num1 + num2))

echo "The sum of $num1 and $num2 is: $sum"


Q2.
#!/bin/bash

echo "Enter a sentence:"


read sentence

word_count=$(echo $sentence | wc -w)

echo "Word count: $word_count"

Q3.
#!/bin/bash

echo "Enter the filename:"


read filename

if [ -f "$filename" ]; then
echo "$(date)" >> "$filename"
echo "Date and time appended to $filename"
else
echo "File does not exist."
Fi

Q4.
#!/bin/bash

echo "Enter the file or directory name:"


read name

if [ -f "$name" ]; then
echo "$name is a regular file."
ls -l "$name"
elif [ -d "$name" ]; then
echo "$name is a directory."
ls -ld "$name"
else
echo "$name does not exist."
Fi

Q5.
#!/bin/bash

if [ $# -ne 2 ]; then
echo "Usage: $0 <directory_path> <days_old>"
exit 1
fi

dir_path=$1
days_old=$2

if [ ! -d "$dir_path" ]; then
echo "Error: Directory does not exist."
exit 1
fi

echo "Cleaning up directory: $dir_path"


echo "Removing files older than $days_old days..."

file_count=$(find "$dir_path" -type f -mtime +"$days_old" -print -delete | wc -l)


echo "$file_count files removed."

echo "Removing empty directories..."


dir_count=$(find "$dir_path" -type d -empty -print -delete | wc -l)
echo "$dir_count empty directories removed."
echo "Cleanup completed."

You might also like