1.
Write a shell script to check if the number entered at the command line is prime or
not.
Ans :-
#!/bin/bash
# Script to check if a number is prime or not
# Check if a number is provided
if [ -z "$1" ]; then
echo "Usage: $0 <number>"
exit 1
fi
num=$1
# Check if input is a positive integer
if ! [[ "$num" =~ ^[0-9]+$ ]]; then
echo "Error: Please enter a positive integer."
exit 1
fi
# 0 and 1 are not prime
if [ "$num" -le 1 ]; then
echo "$num is not a prime number."
exit 0
fi
# Assume prime until proven otherwise
is_prime=1
# Loop from 2 to sqrt(num)
for ((i=2; i*i<=num; i++))
do
if [ $((num % i)) -eq 0 ]; then
is_prime=0
break
fi
done
# Display result
if [ $is_prime -eq 1 ]; then
echo "$num is a prime number."
else
echo "$num is not a prime number."
fi
Example ➖
$ bash check_prime.sh 7
7 is a prime number.
$ bash check_prime.sh 12
12 is not a prime number.
2. Write a shell script to modify “cal” command to display calendars of the specified
months.
Ans:- #!/bin/bash
# Script to modify 'cal' command to display calendars of specified months
# Check if at least one argument is given
if [ $# -eq 0 ]; then
echo "Usage: $0 <month1> [month2] [month3] ..."
echo "Example: $0 1 5 12"
exit 1
fi
# Get the current year
year=$(date +%Y)
# Loop through all the months passed as arguments
for month in "$@"
do
# Check if month is a valid number (1–12)
if [ "$month" -ge 1 ] && [ "$month" -le 12 ]; then
echo "==============================="
echo " Calendar for Month $month, $year"
echo "==============================="
cal "$month" "$year"
echo ""
else
echo "Invalid month: $month (Please enter a number between 1 and 12)"
fi
done
3. Write a shell script to modify “cal” command to display calendars of the specified
range of
months.
Ans :-
#!/bin/bash
# Script to modify 'cal' command to display calendars for a range of months
# Check if two arguments are provided
if [ $# -ne 2 ]; then
echo "Usage: $0 <start_month> <end_month>"
echo "Example: $0 3 6 # Displays March to June of current year"
exit 1
fi
start=$1
end=$2
year=$(date +%Y)
# Validate month numbers
if ! [[ "$start" =~ ^[0-9]+$ && "$end" =~ ^[0-9]+$ ]]; then
echo "Error: Please enter valid numeric values for months (1–12)."
exit
4. Write a shell script to accept a login name. If not a valid login name display message
“Entered login name is invalid”.
Ans :-
#!/bin/bash
# Script to check if a login name (username) is valid
# Prompt the user for a login name
read -p "Enter a login name: " login
# Check if input is empty
if [ -z "$login" ]; then
echo "No login name entered."
exit 1
fi
# Use 'getent passwd' to check if the login exists in the system
if getent passwd "$login" > /dev/null 2>&1; then
echo "Login name '$login' is valid."
else
echo "Entered login name is invalid."
fi
5. Write a shell script to display date in the mm/ dd/ yy format.
Ans :-
#!/bin/bash
# Script to display the current date in mm/dd/yy format
# Use the date command with custom formatting
date +"%m/%d/%y"
6. Write a shell script to display on the screen sorted output of “who” command along
with
the total number of users.
Ans : -
#!/bin/bash
# Script to display sorted output of 'who' command and total number of users
echo "----------------------------------------"
echo " Sorted list of currently logged-in users"
echo "----------------------------------------"
# Display sorted list of users
who | sort
echo "----------------------------------------"
# Count total number of logged-in users
total_users=$(who | wc -l)
echo "Total number of users logged in: $total_users"
echo "----------------------------------------"
7. Write a shell script to display the multiplication table of any number.
ANS :-
#!/bin/bash
# Script to display the multiplication table of any number
# Prompt the user for a number
read -p "Enter a number: " num
# Validate input (check if it’s a number)
if ! [[ "$num" =~ ^[0-9]+$ ]]; then
echo "Error: Please enter a valid positive integer."
exit 1
fi
echo "Multiplication Table of $num"
echo "---------------------------"
# Loop from 1 to 10 and display table
for ((i=1; i<=10; i++))
do
echo "$num x $i = $((num * i))"
done
8. Write a shell script to compare two files and if found equal asks the user to delete the
duplicate file.
Ans :-
#!/bin/bash
# Script to compare two files and delete duplicate if identical
# Check if exactly two filenames are given
if [ $# -ne 2 ]; then
echo "Usage: $0 <file1> <file2>"
exit 1
fi
file1=$1
file2=$2
# Check if both files exist
if [ ! -f "$file1" ]; then
echo "Error: '$file1' does not exist."
exit 1
fi
if [ ! -f "$file2" ]; then
echo "Error: '$file2' does not exist."
exit 1
fi
# Compare files using 'cmp' (binary comparison)
if cmp -s "$file1" "$file2"; then
echo "The files '$file1' and '$file2' are identical."
# Ask user if they want to delete the duplicate file
read -p "Do you want to delete one of them? (y/n): " choice
if [[ "$choice" =~ ^[Yy]$ ]]; then
echo "Which file do you want to delete?"
echo "1) $file1"
echo "2) $file2"
read -p "Enter your choice (1 or 2): " option
case $option in
1)
rm "$file1"
echo "'$file1' deleted."
;;
2)
rm "$file2"
echo "'$file2' deleted."
;;
*)
echo "Invalid choice. No file deleted."
;;
esac
else
echo "No file deleted."
fi
else
echo "The files '$file1' and '$file2' are different."
fi
[Link] a shell script to find the sum of digits of a given number.
Ans :- #!/bin/bash
# Script to find the sum of digits of a given number
# Read a number from user
read -p "Enter a number: " num
# Validate input (must be a positive integer)
if ! [[ "$num" =~ ^[0-9]+$ ]]; then
echo "Error: Please enter a valid positive integer."
exit 1
fi
sum=0
n=$num
# Loop to extract each digit and add to sum
while [ $n -gt 0 ]
do
digit=$((n % 10)) # Get the last digit
sum=$((sum + digit)) # Add it to sum
n=$((n / 10)) # Remove the last digit
done
echo "The sum of digits of $num is: $sum"
10. Write a shell script to merge the contents of three files, sort the contents and then
display
them page by page.
ANS :- #!/bin/bash
# Script to merge the contents of three files, sort them, and display page by page
# Check if three filenames are provided
if [ $# -ne 3 ]; then
echo "Usage: $0 <file1> <file2> <file3>"
exit 1
fi
file1=$1
file2=$2
file3=$3
# Check if all files exist
for f in "$file1" "$file2" "$file3"
do
if [ ! -f "$f" ]; then
echo "Error: File '$f' does not exist."
exit 1
fi
done
# Merge, sort, and display using 'more'
echo "-----------------------------------------"
echo "Merged and Sorted Contents (Page by Page)"
echo "-----------------------------------------"
cat "$file1" "$file2" "$file3" | sort | more
11. Write a shell script to find the LCD (least common divisor) of two numbers.
ANS :-
#!/bin/bash
# Script to find the LCM of two numbers
# Prompt user for two numbers
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
# Validate input
if ! [[ "$num1" =~ ^[0-9]+$ && "$num2" =~ ^[0-9]+$ ]]; then
echo "Error: Please enter valid positive integers."
exit 1
fi
# Function to find GCD using Euclidean algorithm
gcd() {
a=$1
b=$2
while [ $b -ne 0 ]
do
temp=$b
b=$((a % b))
a=$temp
done
echo $a
}
# Calculate GCD
gcd_val=$(gcd $num1 $num2)
# Calculate LCM
lcm=$(( (num1 * num2) / gcd_val ))
echo "The LCM of $num1 and $num2 is: $lcm"