Shell & Kernel Tutorial - Shell Script Answers
Shell Scripts for Given Problems
Sum of two numbers
#!/bin/bash
echo "Enter two numbers:"
read a b
sum=$((a + b))
echo "Sum: $sum"
Perform an Arithmetic Operation Based on User Input
#!/bin/bash
echo "Enter two numbers:"
read a b
echo "Enter operation (+, -, *, /):"
read op
case $op in
+) echo "Result: $((a + b))";;
-) echo "Result: $((a - b))";;
\*) echo "Result: $((a * b))";;
/) echo "Result: $((a / b))";;
*) echo "Invalid operation";;
esac
3.Check If a File Exists or Not
#!/bin/bash
echo "Enter file name:"
read file
if [ -f "$file" ]; then
echo "File exists."
else
echo "File does not exist."
fi
4.Check If a Directory Exists or Not
#!/bin/bash
echo "Enter directory name:"
read dir
if [ -d "$dir" ]; then
echo "Directory exists."
else
echo "Directory does not exist."
fi
5.Check if a Given Number is Even or Odd
#!/bin/bash
echo "Enter a number:"
read num
if (( num % 2 == 0 )); then
echo "Even number"
else
echo "Odd number"
fi
6.Check if a Given Number is Positive or Negative
#!/bin/bash
echo "Enter a number:"
read num
if (( num > 0 )); then
echo "Positive number"
elif (( num < 0 )); then
echo "Negative number"
else
echo "Zero"
fi
7.Find the Length of a String
#!/bin/bash
echo "Enter a string:"
read str
echo "Length of string: ${#str}"
Convert All Uppercase Letters in a String to Lowercase
#!/bin/bash
echo "Enter a string:"
read str
echo "Lowercase: $(echo $str | tr 'A-Z' 'a-z')"
9.Check if Two Strings are Equal
#!/bin/bash
echo "Enter two strings:"
read str1 str2
if [ "$str1" == "$str2" ]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
10.Reverse a String
#!/bin/bash
echo "Enter a string:"
read str
echo "Reversed: $(echo $str | rev)"
11.Find the Factorial of a Number
#!/bin/bash
echo "Enter a number:"
read num
fact=1
for (( i=1; i<=num; i++ )); do
fact=$((fact * i))
done
echo "Factorial: $fact"
12.Check if a Number is Prime
#!/bin/bash
echo "Enter a number:"
read num
if (( num < 2 )); then
echo "Not a prime number"
exit
fi
for (( i=2; i*i<=num; i++ )); do
if (( num % i == 0 )); then
echo "Not a prime number"
exit
fi
done
echo "Prime number"
13.Convert Fahrenheit to Celsius
#!/bin/bash
echo "Enter temperature in Fahrenheit:"
read f
c=$(echo "scale=2; ($f - 32) * 5 / 9" | bc)
echo "Celsius: $c"
14.Calculate the Area of a Rectangle
#!/bin/bash
echo "Enter length and width:"
read l w
echo "Area: $((l * w))"
15.Calculate Grade with Number
#!/bin/bash
echo "Enter marks:"
read marks
if (( marks >= 90 )); then
echo "Grade: A"
elif (( marks >= 80 )); then
echo "Grade: B"
elif (( marks >= 70 )); then
echo "Grade: C"
elif (( marks >= 60 )); then
echo "Grade: D"
else
echo "Grade: F"
fi
16.Delete a Given File If It Exists
#!/bin/bash
echo "Enter filename:"
read file
if [ -f "$file" ]; then
rm "$file"
echo "File deleted."
else
echo "File does not exist."
fi
17.Find if a Given Year is a Leap Year
#!/bin/bash
echo "Enter year:"
read year
if (( year % 400 == 0 || (year % 4 == 0 && year % 100 != 0) )); then
echo "Leap year"
else
echo "Not a leap year"
fi
18.Find the Day of a Birthday
#!/bin/bash
echo "Enter date (YYYY-MM-DD):"
read date
day=$(date -d "$date" '+%A')
echo "Day of birth: $day"
19.Find the Age
#!/bin/bash
echo "Enter birth year:"
read birth_year
current_year=$(date +%Y)
age=$((current_year - birth_year))
echo "Your age is: $age years"
20.Capitalize the First Letter of a Word
#!/bin/bash
echo "Enter a word:"
read word
capitalized=$(echo "$word" | sed -E 's/(^| )([a-z])/\1\U\2/g')
echo "Capitalized: $capitalized"
21.Print Numbers From 5 to 1
#!/bin/bash
for ((i=5; i>=1; i--)); do
echo "$i"
done
22.Print Even Numbers From 1 to 10
#!/bin/bash
for ((i=2; i<=10; i+=2)); do
echo "$i"
done
23.Print the Multiplication Table of a Number
#!/bin/bash
echo "Enter a number:"
read num
for ((i=1; i<=10; i++)); do
echo "$num x $i = $((num * i))"
done
24.Calculate the Sum of Digits of a Given Number
#!/bin/bash
echo "Enter a number:"
read num
sum=0
while [ $num -gt 0 ]; do
digit=$((num % 10))
sum=$((sum + digit))
num=$((num / 10))
done
echo "Sum of digits: $sum"
25.Calculate the Sum of the First “n” Numbers
#!/bin/bash
echo "Enter a number (n):"
read n
sum=0
for ((i=1; i<=n; i++)); do
sum=$((sum + i))
done
echo "Sum of the first $n numbers is: $sum"
26.Find the Smallest and Largest Elements in an Array
#!/bin/bash
echo "Enter numbers separated by space:"
read -a arr
smallest=${arr[0]}
largest=${arr[0]}
for num in "${arr[@]}"; do
if (( num < smallest )); then
smallest=$num
fi
if (( num > largest )); then
largest=$num
fi
done
echo "Smallest number: $smallest"
echo "Largest number: $largest"
27.Calculate the Average of an Array of Numbers
#!/bin/bash
echo "Enter numbers separated by space:"
read -a arr
sum=0
for num in "${arr[@]}"; do
sum=$((sum + num))
done
count=${#arr[@]}
average=$(echo "scale=2; $sum / $count" | bc)
echo "Average: $average"
28.Find the Length of an Array
#!/bin/bash
echo "Enter elements separated by space:"
read -a arr
echo "Length of the array: ${#arr[@]}"
29.Check if a String is a Palindrome
#!/bin/bash
echo "Enter a string:"
read str
rev_str=$(echo "$str" | rev)
if [ "$str" == "$rev_str" ]; then
echo "The string is a palindrome."
else
echo "The string is not a palindrome."
fi
30.Menu Driven Program for File Operations
#!/bin/bash
while true; do
echo "1. Copy a file"
echo "2. Rename a file"
echo "3. Delete a file"
echo "4. Compare two files"
echo "5. Count the number of characters in a file"
echo "6. Change file permissions"
echo "7. Exit"
read -p "Enter choice: " choice
case $choice in
1) echo "Enter source file and destination file:"
read src dest
cp "$src" "$dest"
echo "File copied." ;;
2) echo "Enter old filename and new filename:"
read old new
mv "$old" "$new"
echo "File renamed." ;;
3) echo "Enter filename to delete:"
read file
rm "$file"
echo "File deleted." ;;
4) echo "Enter two filenames to compare:"
read file1 file2
diff "$file1" "$file2" ;;
5) echo "Enter filename:"
read file
wc -m < "$file" ;;
6) echo "Enter filename and permissions (e.g., 755):"
read file perms
chmod "$perms" "$file"
echo "Permissions changed." ;;
7) exit ;;
*) echo "Invalid choice." ;;
esac
done
AWK Program to Print a Set of Data
#!/bin/bash
echo "Printing a set of data using AWK:"
awk 'BEGIN {print "ID Name Marks"; print "1 Alice 85"; print "2 Bob 90";
print "3 Charlie 78"}'