0% found this document useful (0 votes)
8 views4 pages

OSLab 3

The document contains a series of shell scripts created by Taaha Khan for various tasks. These scripts include adding two numbers, counting words in a sentence, appending the current date and time to a file, and checking the type of a file or directory. Each script prompts the user for input and performs specific operations based on that input.

Uploaded by

Taaaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

OSLab 3

The document contains a series of shell scripts created by Taaha Khan for various tasks. These scripts include adding two numbers, counting words in a sentence, appending the current date and time to a file, and checking the type of a file or directory. Each script prompts the user for input and performs specific operations based on that input.

Uploaded by

Taaaha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd

Name: Taaha Khan

NU ID: 23K-0583

IN-LAB TASKS
Question1:

t.sh:
echo "Adding Two Numbers:-"
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2

echo "num1 = " $num1


echo "num2 = " $num2

(( ANS=$num1+$num2 ))
echo "Answer: $ANS"
Question2:

q2.sh:
echo "Enter a sentence:"
read sentence

count=0

for word in $sentence; do


((count++))
done

echo "Number of words in the sentence: $count"


Question3:

q3.sh:
echo "Enter file name:"
read filename

if [[ -f "$filename" ]];then
dateTime=$(date "+%Y-%m-%d %H:%M:%S")

echo "Current Date and Time: $dateTime" >> "$filename"

echo "Date and Time has been appended to $filename"


else
echo "The file $filename doesn't exist."
fi
Question4:

q4.sh:
echo "Please enter the name of the file or directory:"
read file

if [ -z "$file" ]; then
echo "No file or directory name provided."
exit 1
fi

if [ -f "$file" ]; then
echo "$file is a regular file."
elif [ -d "$file" ]; then
echo "$file is a directory."
elif [ -e "$file" ]; then
echo "$file exists, but it is neither a regular file nor a directory."
else
echo "$file does not exist."
fi

You might also like