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