0% found this document useful (0 votes)
21 views30 pages

Shell Scripts for Unix Programming Tasks

The document contains a series of shell scripts that perform various tasks such as checking for prime numbers, modifying the 'cal' command, validating login names, displaying dates, sorting user lists, and performing basic arithmetic operations. Each script includes input prompts, error handling, and specific functionalities like calculating factorials, finding the greatest number among three, and checking for Armstrong numbers. The document serves as a practical guide for Unix programming lab exercises.

Uploaded by

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

Shell Scripts for Unix Programming Tasks

The document contains a series of shell scripts that perform various tasks such as checking for prime numbers, modifying the 'cal' command, validating login names, displaying dates, sorting user lists, and performing basic arithmetic operations. Each script includes input prompts, error handling, and specific functionalities like calculating factorials, finding the greatest number among three, and checking for Armstrong numbers. The document serves as a practical guide for Unix programming lab exercises.

Uploaded by

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

DSE–1 Practical: Unix Programming Lab 1.

Write a shell
script to check if the number entered at the command
line is prime or not.
#!/bin/bash

# Check if number is entered


if [ -z "$1" ]; then
echo "Please enter a number!"
exit 1
fi

num=$1
flag=0

# 0 and 1 are not prime


if [ $num -le 1 ]; then
echo "$num is NOT a prime number"
exit 0
fi

# Check divisibility
for ((i=2; i<=num/2; i++))
do
if [ $((num % i)) -eq 0 ]; then
flag=1
break
fi
done

if [ $flag -eq 1 ]; then


echo "$num is NOT a prime number"
else
echo "$num is a PRIME number"
fi
Input:
./[Link] 17
Output:
17 is a PRIME number

[Link] a shell script to modify “cal” command to


display calendars of the specified months.
With op
#!/bin/bash

# Check at least one month is entered


if [ $# -lt 1 ]; then
echo "Usage: $0 month [month ...] [year]"
exit 1
fi

# Default year = current year


year=$(date +%Y)

# If last argument is a 4-digit number → treat it as year


last=${!#}
if [[ $last =~ ^[0-9]{4}$ ]]; then
year=$last
# Reduce argument count (ignore last year argument)
set -- "${@:1:$(($#-1))}"
fi

# Loop through all months entered


for m in "$@"
do
echo "------------------------------"
echo " Calendar for $m / $year"
echo "------------------------------"
cal $m $year
echo
done

Input
./[Link] 3
Output
------------------------------
Calendar for 3 / 2025
------------------------------
March 2025
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
3. Write a shell script to modify “cal” command to
display calendars of the specified range of
months.
#!/bin/bash

# Check if 2 arguments are given


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

start=$1
end=$2
year=$(date +%Y)

# Validate months
if [ $start -lt 1 ] || [ $start -gt 12 ] || [ $end -lt 1 ] || [
$end -gt 12 ]; then
echo "Months must be between 1 and 12."
exit 1
fi

if [ $start -gt $end ]; then


echo "Start month should be less than or equal to end
month."
exit 1
fi

# Display calendars
for ((m=start; m<=end; m++))
do
echo "---------------------------------------"
echo " Calendar for Month $m, $year"
echo "---------------------------------------"
cal $m $year
echo
done

Input
./[Link] 3 5
Output
--------------------------------------
-
Calendar for Month 3, 2025
--------------------------------------
-
March 2025
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

--------------------------------------
-
Calendar for Month 4, 2025
--------------------------------------
-
April 2025
Su Mo Tu We Th Fr Sa
...

--------------------------------------
-
Calendar for Month 5, 2025
--------------------------------------
-
May 2025
Su Mo Tu We Th Fr Sa

[Link] a shell script to accept a login name. If not a


valid login name display message
“Entered login name is invalid”.
#!/bin/bash

echo -n "Enter login name: "


read uname

# Check if user exists in /etc/passwd


grep "^$uname:" /etc/passwd > /dev/null

if [ $? -eq 0 ]
then
echo "Valid login name."
else
echo "Entered login name is invalid."
Fi

Input
Enter login name: root
Output
Valid login name.

5. Write a shell script to display date in the mm/ dd/ yy


format.
#!/bin/bash

# Display date in mm/dd/yy format


date +"%m/%d/%y"
./[Link]
Output
swift
Copy code
03/08/25
6. Write a shell script to display on the screen sorted
output of “who” command along with
the total number of users.
Output
Sorted list of logged-in users:
adarsha tty2 2025-03-08 10:15
root pts/0 2025-03-08 10:16
student pts/1 2025-03-08 10:20

Total number of users logged in: 3


7. Write a shell script to display the multiplication table
of any number.
#!/bin/bash

echo -n "Enter a number: "


read num
echo "Multiplication Table of $num"
for ((i=1; i<=10; i++))
do
echo "$num x $i = $((num * i))"
done

Output
Enter a number: 7
Multiplication Table of 7
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

8. Write a shell script to compare two files and if found


equal asks the user to delete the
duplicate file
#!/bin/bash

echo -n "Enter first filename: "


read file1

echo -n "Enter second filename: "


read file2

# Check if both files exist


if [ ! -f "$file1" ] || [ ! -f "$file2" ]; then
echo "One or both files do not exist."
exit 1
fi

# Compare files
cmp -s "$file1" "$file2"
if [ $? -eq 0 ]; then
echo "Files are equal."

echo -n "Do you want to delete the duplicate file


$file2? (y/n): "
read ans

if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then


rm "$file2"
echo "$file2 deleted."
else
echo "File not deleted."
fi
else
echo "Files are NOT equal."
Fi
Input
Enter first filename: [Link]
Enter second filename: [Link]
Output
Files are equal.
Do you want to delete the duplicate
file [Link]? (y/n): y
[Link] deleted.

9. Write a shell script to find the sum of digits of a given


number.
#!/bin/bash

echo -n "Enter a number: "


read num

sum=0
n=$num
while [ $n -gt 0 ]
do
digit=$(( n % 10 )) # Get last digit
sum=$(( sum + digit ))
n=$(( n / 10 )) # Remove last digit
done

echo "Sum of digits of $num = $sum"

Output
Enter a number: 548
Sum of digits of 548 = 17

10. Write a shell script to merge the contents of three


files, sort the contents and then display
them page by page.
#!/bin/bash

echo -n "Enter first filename: "


read f1

echo -n "Enter second filename: "


read f2

echo -n "Enter third filename: "


read f3

# Check if all files exist


if [ ! -f "$f1" ] || [ ! -f "$f2" ] || [ ! -f "$f3" ]; then
echo "One or more files do not exist."
exit 1
fi

# Merge, sort, and display page by page


cat "$f1" "$f2" "$f3" | sort | more
Input Files

[Link]
banana
apple
cat
[Link]
dog
apple
zebra
[Link]
lion
ball
cat

Output
apple
apple
banana
ball
cat
cat
dog
lion
zebra

11. Write a shell script to find the LCD (least common


divisor) of two numbers.
#!/bin/bash

echo -n "Enter first number: "


read a

echo -n "Enter second number: "


read b

# Find the smaller number


if [ $a -lt $b ]; then
min=$a
else
min=$b
fi

lcd=1

# Find the least common divisor


for ((i=2; i<=min; i++))
do
if [ $((a % i)) -eq 0 ] && [ $((b % i)) -eq 0 ]; then
lcd=$i
break
fi
done

echo "LCD of $a and $b = $lcd"

Input
Enter first number: 12
Enter second number: 18
Output
LCD of 12 and 18 = 2

12. Write a shell script to perform the tasks of basic


calculator.
#!/bin/bash

echo "===== BASIC CALCULATOR ====="


echo "1. Addition"
echo "2. Subtraction"
echo "3. Multiplication"
echo "4. Division"

echo -n "Enter your choice (1-4): "


read choice
echo -n "Enter first number: "
read a

echo -n "Enter second number: "


read b

case $choice in
1)
result=$((a + b))
echo "Result = $result"
;;
2)
result=$((a - b))
echo "Result = $result"
;;
3)
result=$((a * b))
echo "Result = $result"
;;
4)
if [ $b -eq 0 ]; then
echo "Error: Division by zero not allowed!"
else
result=$(echo "scale=2; $a / $b" | bc)
echo "Result = $result"
fi
;;
*)
echo "Invalid choice!"
;;
Esac

Output
===== BASIC CALCULATOR =====
1. Addition
2. Subtraction
3. Multiplication
4. Division
Enter your choice (1-4): 3
Enter first number: 7
Enter second number: 8
Result = 56

13. Write a shell script to find the power of a given


number.
#!/bin/bash

echo -n "Enter base number: "


read base

echo -n "Enter exponent: "


read exp

result=1
for ((i=1; i<=exp; i++))
do
result=$(( result * base ))
done

echo "$base raised to the power $exp = $result"

Output
Enter base number: 5
Enter exponent: 3
5 raised to the power 3 = 125

14. Write a shell script to find the greatest number


among the three numbers.
#!/bin/bash

echo -n "Enter first number: "


read a
echo -n "Enter second number: "
read b

echo -n "Enter third number: "


read c

if [ $a -ge $b ] && [ $a -ge $c ]; then


echo "Greatest number is: $a"
elif [ $b -ge $a ] && [ $b -ge $c ]; then
echo "Greatest number is: $b"
else
echo "Greatest number is: $c"
fi

Output
Enter first number: 12
Enter second number: 45
Enter third number: 27
Greatest number is: 45
15. Write a shell script to find the factorial of a given
number.
#!/bin/bash

echo -n "Enter a number: "


read n

fact=1

for ((i=1; i<=n; i++))


do
fact=$((fact * i))
done

echo "Factorial of $n = $fact"


Output
Enter a number: 6
Factorial of 6 = 720

16. Write a shell script to check whether the number is


Armstrong or not.
#!/bin/bash

echo "Enter a number:"


read num

orig=$num
sum=0

while [ $num -gt 0 ]


do
digit=$(( num % 10 )) # extract digit
cube=$(( digit * digit * digit )) # cube of digit
sum=$(( sum + cube ))
num=$(( num / 10 )) # remove last digit
done

if [ $sum -eq $orig ]


then
echo "$orig is an Armstrong number"
else
echo "$orig is NOT an Armstrong number"
fi

Output
Enter a number:
153
153 is an Armstrong number

You might also like