LINUX SHELL PROGRAMMING LAB
LINUX SHELL PROGRAMMING LAB
1. Use of Basic Unix Shell Commands: ls, mkdir, rmdir, cd, cat,
banner, touch, file, wc, sort, cut, grep, dd, dfspace, du, ulimit.
Ans:-
Example: cut -d"," -f1 myfile.csv (extracts the first field from a
comma-separated file)
grep: Searches for a pattern in files.
Ans:-
ls -i: Displays the inode number along with the file or directory
names.
Code:-
#!/bin/bash
Expected Output:-
Enter a year:
Input: 2024
Output: 2024 is a leap year.
To input angles of a triangle and find out whether it is a valid triangle
or not:
Enter a character:
Input: A
Output: The character is an alphabet.
To calculate profit or loss:
Code:-
#!/bin/bash
# 4.1 Write a shell script to print all even and odd numbers from 1 to
10
echo "Even numbers:"
for ((i=1; i<=10; i++))
do
if [ $((i % 2)) -eq 0 ]; then
echo $i
fi
done
factorial=1
counter=1
while [ $counter -le $number ]
do
factorial=$((factorial * counter))
counter=$((counter + 1))
done
echo "Factorial of $number is: $factorial"
# 4.4 Write a shell script to print the sum of all even numbers from 1
to 10
sum=0
for ((i=1; i<=10; i++))
do
if [ $((i % 2)) -eq 0 ]; then
sum=$((sum + i))
fi
done
echo "Sum of even numbers from 1 to 10: $sum"
# 4.5 Write a shell script to print the sum of digits of any number
echo "Enter a number:"
read number
sum=0
while [ $number -gt 0 ]
do
digit=$((number % 10))
sum=$((sum + digit))
number=$((number / 10))
done
echo "Sum of digits: $sum"
Expected Output:-
Write a shell script to print all even and odd numbers from 1 to 10:
Even numbers:
Output: 2 4 6 8 10
Odd numbers:
Output: 1 3 5 7 9
Write a shell script to print the table of a given number:
Enter a number:
Input: 5
Output:
Table of 5:
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
Write a shell script to calculate the factorial of a given number:
Enter a number:
Input: 4
Output: Factorial of 4 is: 24
Write a shell script to print the sum of all even numbers from 1 to 10:
Enter a number:
Input: 12345
Output: Sum of digits: 15
Code:-
#!/bin/bash
case $choice in
1)
result=$((num1 + num2))
echo "Addition: $num1 + $num2 = $result"
;;
2)
result=$((num1 - num2))
echo "Subtraction: $num1 - $num2 = $result"
;;
3)
result=$((num1 * num2))
echo "Multiplication: $num1 x $num2 = $result"
;;
4)
if [ $num2 -eq 0 ]; then
echo "Error: Division by zero not allowed."
else
result=$((num1 / num2))
echo "Division: $num1 / $num2 = $result"
fi
;;
*)
echo "Invalid choice!"
;;
esac
case $day in
1)
echo "Sunday"
;;
2)
echo "Monday"
;;
3)
echo "Tuesday"
;;
4)
echo "Wednesday"
;;
5)
echo "Thursday"
;;
6)
echo "Friday"
;;
7)
echo "Saturday"
;;
*)
echo "Invalid number!"
;;
esac
month=$((month + 1))
done
Expected Output:-
Write a shell script to make a basic calculator:
Code:-
#!/bin/bash
echo
}
decimalToBinary() {
local decimal=$1
local binary=""
# Main program
Code:-
#!/bin/bash
echo
spaces=$((spaces - 1))
stars=$((stars + 2))
done
spaces=1
stars=$((stars - 2))
for ((i=1; i<=n-1; i++))
do
for ((j=1; j<=spaces; j++))
do
echo -n " "
done
echo
spaces=$((spaces + 1))
stars=$((stars - 2))
done
}
echo
done
}
echo
done
}
echo
done
}
# Main program
Code:-
#include <stdio.h>
int main() {
int arr[100], size, i;
return 0;
}
Expected Output:
#include <stdio.h>
int main() {
int arr[100], size, i, sum = 0;
return 0;
}
Expected Output:
#include <stdio.h>
int main() {
int arr[100], size, i;
return 0;
}
Expected Output:
#include <stdio.h>
int main() {
int arr[100], size, i, element, found = 0;
if (found) {
printf("Element %d found at index %d\n", element, i);
} else {
printf("Element %d not found in the array\n", element);
}
return 0;
}
Expected Output:
mathematica
Copy code
Enter the size of the array: 5
Enter array elements:
10
20
30
40
50
Enter the element to search: 30
Element 30 found at index 2
int main() {
int arr[100], size, i, order;
if (order == 0) {
printf("Array elements in ascending order: ");
} else {
printf("Array elements in descending order: ");
}
return 0;
}in ascending order: 10 20 30 40 50
Expected Output:-
Familiarity with basic Unix shell commands for file and directory
manipulation.
Understanding of common shell commands such as ls, mkdir,
rmdir, cd, cat, touch, etc.
Ability to use commands like file, wc, sort, cut, grep for file
analysis and manipulation.
Knowledge of commands like dd, du, dfspace for disk
management and utilization.
Understanding of ulimit command for resource limitation.