ASSIGNMENT - 2
Ques 1. Write a shell script that accepts a single character as input and determines
whether it is:
(i) a lowercase alphabet
(ii) an uppercase alphabet
(iii) a digit
(iv) a special symbol
(v) a vowel
Code :
#!/bin/bash
# Ask the user to enter a single character
read -p "Enter a single character: " char
# Check if input is exactly one character
if [ ${#char} -ne 1 ]; then
echo "Error: Please enter only one character."
exit 1
fi
# Check for lowercase alphabet
if [[ "$char" =~ [a-z] ]]; then
echo "It is a lowercase alphabet."
else
echo "It is not a lowercase alphabet."
fi
# Check for uppercase alphabet
if [[ "$char" =~ [A-Z] ]]; then
echo "It is an uppercase alphabet."
else
echo "It is not an uppercase alphabet." fi
# Check for digit
if [[ "$char" =~ [0-9] ]]; then
echo "It is a digit."
else
echo "It is not a digit."
fi
# Check for special symbol (not letter or digit) if [[ "$char" =~ [^a-zA-
Z0-9] ]]; then
echo "It is a special symbol."
else
echo "It is not a special symbol."
fi
# Check for vowel (both lowercase and uppercase) if [[ "$char" =~
[aeiouAEIOU] ]]; then
echo "It is a vowel."
else
echo "It is not a vowel."
fi
Output
Ques 2. Write a shell script utilizing the case .. esac control structure to perform the
following tasks:
(i) Find and display the number of users currently logged into the
system (ii) Print the calendar for the current year
(iii) Display the current date and time
Code :
#!/bin/bash
echo "Choose an option:"
echo "1) Number of users logged in"
echo "2) Calendar for current year"
echo "3) Print the date"
read -p "Enter your choice (1-3): " choice
case $choice in
1)
echo "Number of users logged in:"
who | wc -l
;;
2)
echo "Calendar for the current year:"
cal -y
;;
3)
echo "Current date is:"
date
;;
*)
echo "Invalid choice! Please enter 1, 2, or 3."
;;
esac
Output
Ques 3. Write a shell script to check whether the specified path refers to a directory or
not.
Code :
#!/bin/bash
read -p "Enter the file or directory name: " filename
if [ -d "$filename" ]; then
echo "'$filename' is a directory."
else
echo "'$filename' is NOT a directory."
fi
Output
Ques 4. Write a shell script to count and display the number of files present within a
specified directory.
Code :
#!/bin/bash
echo "Enter the directory path"
read dir
#check if the directory exists
if [ -d "$dir" ]; then
#count only files(not directories)
#file_count=$(find "$dir"-type -f | wc-l)
#echo "Number of files in '$dir':$file_count"
count=$( ls -l "$dir" | grep ^- | wc -l )
echo "total file: $count"
else
echo "Directory does not exist"
fi
Output
Ques 5. Write a shell script to copy the contents of one file to another file.
Code :
#!/bin/bash
read -p "Enter source file name: " source
read -p "Enter destination file name: " dest
if [ ! -f "$source" ]; then
echo "Source file does not exist."
exit 1
fi
cp "$source" "$dest"
echo "Contents copied from '$source' to '$dest'."
Output
Ques 6. Write a shell script to accept two numbers as command-line arguments and
display their sum.
Code :
read -p "Enter first value " num1
read -p "Enter second value " num2
sum=$((num1 + num2))
echo "The sum of $num1 and $num2 is: $sum"
Output
Ques 7. Write a shell script to determine the largest number among three values
supplied as command-line arguments.
Code :
read -p "Enter first number " num1
read -p "Enter second number " num2
read -p "Enter third number " num3
if [ "$num1" -ge "$num2" ] && [ "$num1" -ge "$num3" ]; then
largest=$num1
elif [ "$num2" -ge "$num1" ] && ["$num2" -ge "$num3" ]; then
largest=$num2
else
largest=$num3
fi
echo "the largest number is : $largest"
Output
basic arithmetic operations (+, −, ×, ÷, %) using command-line arguments.
Ques 8. Write a shell script to implement a simple calculator that performs
Code :
#!/bin/bash
if [ $# -ne 3 ];then
echo " usage: $0 num1 operator num2"
echo "operators:+ - x / %"
exit
fi
num1=$1
op=$2
num2=$3
case $op in
+) result=$((num1+num2));;
-) result=$((num1-num2));;
x) result=$((num1*num2));;
/)
if [ "$num2" -eq 0 ];then
echo "error:division by zero"
exit 1
fi
result=$((num1/num2));;
%)
if [ "$num2" -eq 0 ];then
echo "error:Modulo by zero"
exit 1
fi
result=$((num1%num2));;
*)
echo "invalid operator.use +,-,x,/ or %"
exit 1
;;
esac
echo "result:$result"
Output