Shell Scripting Lab Manual
Shell Script to Determine the Area and Perimeter of a Rectangle
Aim:
To write a shell script that calculates the area and perimeter of a rectangle based on user
input.
Algorithm:
- Prompt the user for the length.
- Prompt the user for the breadth.
- Calculate area as length × breadth.
- Calculate perimeter as 2 × (length + breadth).
- Display the results.
Program:
#!/bin/bash
echo "Enter the length:"
read length
echo "Enter the breadth:"
read breadth
area=$((length * breadth))
perimeter=$((2 * (length + breadth)))
echo "Area of Rectangle = $area"
echo "Perimeter of Rectangle = $perimeter"
Sample Output:
Enter the length:
5
Enter the breadth:
4
Area of Rectangle = 20
Perimeter of Rectangle = 18
import [Link];
public class RectangleCalculator {
public static void main(String[] args) {
// Create Scanner object for user input
Scanner sc = new Scanner([Link]);
// Input length and breadth
[Link]("Enter the length of the rectangle: ");
double length = [Link]();
[Link]("Enter the breadth of the rectangle: ");
double breadth = [Link]();
// Calculate area and perimeter
double area = length * breadth;
double perimeter = 2 * (length + breadth);
// Display results
[Link]("Area of Rectangle = " + area);
[Link]("Perimeter of Rectangle = " + perimeter);
// Close the scanner
[Link]();
}
Shell Script to Calculate the Sum and Average of an Array of Numbers
Aim:
To calculate the sum and average of an array entered by the user.
Algorithm:
- Ask the user for number of elements.
- Read elements into an array.
- Calculate sum using a loop.
- Compute average as sum / count.
Program:
#!/bin/bash
echo "Enter number of elements:"
read n
sum=0
echo "Enter the numbers:"
for (( i=0; i<n; i++ ))
do
read num
sum=$((sum + num))
done
avg=$(echo "scale=2; $sum / $n" | bc)
echo "Sum = $sum"
echo "Average = $avg"
Sample Output:
Enter number of elements:
4
Enter the numbers:
5
10
15
20
Sum = 50
Average = 12.50
import [Link];
public class SumAndAverageArray {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Step 1: Take array size from the user
[Link]("Enter the number of elements in the array: ");
int n = [Link]();
// Step 2: Declare an array
int[] numbers = new int[n];
// Step 3: Input array elements
[Link]("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
numbers[i] = [Link]();
// Step 4: Calculate sum
int sum = 0;
for (int num : numbers) {
sum += num; // sum = sum + num
// Step 5: Calculate average
double average = (double) sum / n; // Cast to double for decimal result
// Step 6: Display results
[Link]("Sum of elements = " + sum);
[Link]("Average of elements = " + average);
[Link](); }}
Shell Script to Calculate the Fibonacci Sequence
Aim:
To display the Fibonacci sequence up to a given number of terms.
Algorithm:
- Take number of terms as input.
- Initialize first two terms as 0 and 1.
- Loop to calculate the next terms as sum = a + b.
- Update variables and print sequence.
Program:
#!/bin/bash
echo "Enter number of terms:"
read n
a=0
b=1
echo "Fibonacci Series:"
for (( i=0; i<n; i++ ))
do
echo -n "$a "
fn=$((a + b))
a=$b
b=$fn
done
echo
Sample Output:
Enter number of terms:
6
Fibonacci Series:
011235
import [Link];
public class FibonacciSeries {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Step 1: Take the number of terms from the user
[Link]("Enter the number of terms: ");
int n = [Link]();
// Step 2: Initialize first two terms
int first = 0, second = 1;
// Step 3: Display the series
[Link]("Fibonacci Series: ");
for (int i = 1; i <= n; i++) {
[Link](first + " ");
// Step 4: Calculate next term
int next = first + second;
first = second;
second = next;
[Link]();
}
Shell Script to Find Prime Numbers in a Given Range
Aim:
To find all prime numbers within a user-specified range.
Algorithm:
- Read lower and upper limits from the user.
- Loop through numbers in the range.
- Check divisibility for each number.
- If number is prime, display it.
Program:
#!/bin/bash
echo "Enter lower limit:"
read low
echo "Enter upper limit:"
read high
echo "Prime numbers between $low and $high are:"
for (( num=$low; num<=$high; num++ ))
do
if [ $num -lt 2 ]; then
continue
fi
prime=1
for (( i=2; i<=num/2; i++ ))
do
if [ $((num % i)) -eq 0 ]; then
prime=0
break
fi
done
if [ $prime -eq 1 ]; then
echo -n "$num "
fi
done
echo
Sample Output:
Enter lower limit:
10
Enter upper limit:
20
Prime numbers between 10 and 20 are:
11 13 17 19
import [Link];
public class PrimeNumbersInRange {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Step 1: Take range input from user
[Link]("Enter the starting number: ");
int start = [Link]();
[Link]("Enter the ending number: ");
int end = [Link]();
[Link]("Prime numbers between " + start + " and " + end + " are:");
// Step 2: Loop through each number in the range
for (int num = start; num <= end; num++) {
if (isPrime(num)) {
[Link](num + " ");
[Link]();
}
// Step 3: Method to check if a number is prime
public static boolean isPrime(int number) {
if (number <= 1) {
return false; // 0, 1, and negative numbers are not prime
for (int i = 2; i <= [Link](number); i++) {
if (number % i == 0) {
return false; // divisible by i → not prime
return true; // prime number
}
Shell Script to Check if a String is a Palindrome
Aim:
To determine whether a given string is a palindrome.
Algorithm:
- Read the string from the user.
- Reverse the string using rev.
- Compare original and reversed strings.
- Display result.
Program:
#!/bin/bash
echo "Enter a string:"
read str
revstr=$(echo "$str" | rev)
if [ "$str" == "$revstr" ]; then
echo "$str is a palindrome."
else
echo "$str is not a palindrome."
fi
Sample Output:
Enter a string:
madam
madam is a palindrome.
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
// Step 1: Take input string from the user
[Link]("Enter a string: ");
String str = [Link]();
// Step 2: Convert string to lowercase for case-insensitive comparison
String original = [Link]();
// Step 3: Reverse the string
String reversed = new StringBuilder(original).reverse().toString();
// Step 4: Compare original and reversed strings
if ([Link](reversed)) {
[Link](str + " is a palindrome.");
} else {
[Link](str + " is not a palindrome.");
[Link]();