1.
(A)
#!/bin/sh
echo "Name : "
read a
echo "programme : "
read b
echo "enrolment number : "
read c
echo "$a"
echo "$b"
echo "$c"
b.
#!/bin/sh
echo " enter 4 int : "
read a
read b
read c
read d
add=`expr $a + $b + $c + $d`
product=`expr $a \* $b \* $c \* $d`
avg=`expr $add / 4`
echo $add
echo $product
echo $avg
c.
#!/bin/sh
echo " enter num: "
read a
if [ `expr $a % 2` == 1 ]
then
echo "odd"
else
echo "even"
fi
d.
#!/bin/sh
echo " enter num: "
read a
if [ `expr $a % 2` == 1 ]
then
echo "odd"
else
echo "even"
fi
e.
#!/bin/sh
echo "enter file"
read file
cat $file | grep ['0-9']
f.
#!/bin/sh
echo "enter string 1: "
read a
echo "enter string 2: "
read b
str="$a$b"
echo ${#str}
g.
#!/bin/sh
echo "enter file 1"
read a
echo " enter file 2"
read b
cat $a $b > c.txt
h.
#!/bin/sh
sleep 5
date | tr -s " " | cut -d" " -f5,6
Q2.
#!/bin/bash
echo "Enter length of rectangle:"
read length
echo "Enter breadth of rectangle:"
read breadth
echo "Enter radius of circle:"
read radius
rectangle_area=$(echo "$length * $breadth" | bc)
rectangle_perimeter=$(echo "2 * ($length + $breadth)" | bc)
circle_area=$(echo "3.14 * $radius * $radius" | bc)
circle_circumference=$(echo "2 * 3.14 * $radius" | bc)
echo "Area of Rectangle: $rectangle_area"
echo "Perimeter of Rectangle: $rectangle_perimeter"
echo "Area of Circle: $circle_area"
echo "Circumference of Circle: $circle_circumference"
q3.
#!/bin/bash
echo "Menu:"
echo "1. Addition (+)"
echo "2. Subtraction (-)"
echo "3. Multiplication (*)"
echo "4. Division (/)"
echo "5. Modulus (%)"
echo "6. Increment (++) for First Number"
echo "7. Decrement (--) for First Number"
echo "Enter first number:"
read num1
echo "Enter second number:"
read num2
echo "Enter your choice (1-7):"
read choice
case $choice in
1) result=$(echo "$num1 + $num2" | bc) ;;
2) result=$(echo "$num1 - $num2" | bc) ;;
3) result=$(echo "$num1 * $num2" | bc) ;;
4) result=$(echo "scale=2; $num1 / $num2" | bc) ;;
5) result=$(echo "$num1 % $num2" | bc) ;;
6) result=$((num1 + 1)) ;;
7) result=$((num1 - 1)) ;;
*) echo "Invalid choice"; exit 1 ;;
esac
echo "Result: $result"
q4.
#!/bin/bash
echo "Enter a number to calculate its factorial:"
read number
factorial=1
counter=$number
while [ $counter -gt 0 ]
do
factorial=$((factorial * counter))
counter=$((counter - 1))
done
echo "Factorial of $number is: $factorial"
q4(2).
#!/bin/bash
echo "Enter a number to calculate its factorial:"
read number
factorial=1
for (( i=1; i<=number; i++ ))
do
factorial=$((factorial * i))
done
echo "Factorial of $number is: $factorial"
q5.
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: $0 <file>"
exit 1
fi
file=$1
if [ ! -f "$file" ] || [ ! -r "$file" ]; then
echo "Error: File '$file' does not exist or is not readable."
exit 1
fi
lowest=0
highest=0
while read -r number || [[ -n "$number" ]]; do
number=$(echo "$number" | tr -d '[:space:]')
if [[ -z "$number" || ! "$number" =~ ^[0-9-]+$ ]]; then
continue
fi
if [ -z "$lowest" ] || (( number < lowest )); then
lowest=$number
fi
if [ -z "$highest" ] || (( number > highest )); then
highest=$number
fi
done < "$file"
echo "Lowest number: $lowest"
echo "Highest number: $highest"
q6
#!/bin/bash
echo "Enter the number of elements:"
read n
sum=0
echo "Enter $n numbers (one per line):"
for (( i=0; i<n; i++ ))
do
read num
numbers[$i]=$num
sum=$((sum + num))
done
if [ $n -gt 0 ]; then
average=$(echo "scale=2; $sum / $n" | bc)
else
average=0
fi
echo "Average of the $n numbers is: $average"
8.
#!/bin/bash
read -p "Enter the number of rows: " rows
read -p "Enter the number of columns: " cols
echo "Enter elements for the first $rows x $cols matrix:"
matrix1=()
for (( i = 0; i < rows; i++ )); do
for (( j = 0; j < cols; j++ )); do
read -p "Element [$i,$j]: " element
matrix1[$((i * cols + j))]=$element
done
done
echo "Enter elements for the second $rows x $cols matrix:"
matrix2=()
for (( i = 0; i < rows; i++ )); do
for (( j = 0; j < cols; j++ )); do
read -p "Element [$i,$j]: " element
matrix2[$((i * cols + j))]=$element
done
done
result_matrix=()
for (( i = 0; i < rows; i++ )); do
for (( j = 0; j < cols; j++ )); do
index=$((i * cols + j))
result_matrix[$index]=$((matrix1[$index] + matrix2[$index]))
done
done
echo "The resulting matrix is:"
for (( i = 0; i < rows; i++ )); do
for (( j = 0; j < cols; j++ )); do
echo -n "${result_matrix[$((i * cols + j))]} "
done
echo
done
9.
#!/bin/bash
read -p "Enter the number of rows: " rows
read -p "Enter the number of columns: " cols
echo "Enter elements for the $rows x $cols matrix:"
matrix=()
for (( i = 0; i < rows; i++ )); do
for (( j = 0; j < cols; j++ )); do
read -p "Element [$i,$j]: " element
matrix[$((i * cols + j))]=$element
done
done
transpose=()
for (( i = 0; i < cols; i++ )); do
for (( j = 0; j < rows; j++ )); do
transpose[$((i * rows + j))]=${matrix[$((j * cols + i))]}
done
done
echo "The transposed matrix is:"
for (( i = 0; i < cols; i++ )); do
for (( j = 0; j < rows; j++ )); do
echo -n "${transpose[$((i * rows + j))]} "
done
echo
done
10.
#!/bin/sh
# Check if the last argument is a year
is_year() {
case $1 in
''|*[!0-9]*) return 1 ;; # Not a year
*) return 0 ;; # Is a year
esac
}
# Initialize an empty list of months and set year to current year
months=()
current_year=`date +%Y`
year=$current_year
# Process arguments
for arg in "$@"; do
if is_year "$arg"; then
year=$arg
else
months+=("$arg")
fi
done
# If no months are provided, use the current month
if [ ${#months[@]} -eq 0 ]; then
months+=(`date +%b`)
fi
# Process each month and convert to numeric representation if necessary
for month in "${months[@]}"; do
case $month in
jan*|Jan*) month_num=1 ;;
feb*|Feb*) month_num=2 ;;
mar*|Mar*) month_num=3 ;;
apr*|Apr*) month_num=4 ;;
may*|May*) month_num=5 ;;
jun*|Jun*) month_num=6 ;;
jul*|Jul*) month_num=7 ;;
aug*|Aug*) month_num=8 ;;
sep*|Sep*) month_num=9 ;;
oct*|Oct*) month_num=10 ;;
nov*|Nov*) month_num=11 ;;
dec*|Dec*) month_num=12 ;;
[1-9]|10|11|12) month_num=$month ;;
*) echo "Invalid month: $month"; continue ;;
esac
/usr/bin/cal $month_num $year
done