shellprograms
shellprograms
check_positive_negative() {
if [ $1 -gt 0 ]; then
echo "The number is positive."
elif [ $1 -lt 0 ]; then
echo "The number is negative."
else
echo "The number is zero, neither positive nor negative."
fi
}
echo "Enter a number:"
read number
check_positive_negative $number
10.Write a shell program to find the sum of two numbers using function programming
calculate_sum() {
sum=$(( $1 + $2 ))
echo $sum
}
# Main script
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
result=$(calculate_sum $num1 $num2)
echo "Sum of $num1 and $num2 is: $result"
11. Write a shell program for implementing sequential file allocation strategy
DISK_SIZE=100
current_position=0
allocate_file() {
filename=$1
size=$2
if [ $((current_position + size)) -le $DISK_SIZE ]; then
echo "$filename allocated at position $current_position"
current_position=$((current_position + size))
else
echo "Not enough space to allocate $filename"
fi
}
echo "Sequential File Allocation Strategy"
allocate_file "file1.txt" 20
allocate_file "file2.txt" 30
allocate_file "file3.txt" 15
allocate_file "file4.txt" 40
13. Write a Shell program to find the smallest digit from a number
echo "Atharv Tiwari"
echo "Enter three numbers:"
read num1
read num2
read num3
smallest=$num1
if [ $num2 -lt $smallest ]; then
smallest=$num2
fi
if [ $num3 -lt $smallest ]; then
smallest=$num3
fi
echo "The smallest number is: $smallest"
14. Write a shell program to find the smallest number between two numbers usingfunction
find_smallest_number() {
if [ $1 -lt $2 ]; then
echo $1
else
echo $2
fi
}
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
smallest=$(find_smallest_number $num1 $num2)
echo "The smallest number between $num1 and $num2 is: $smallest"
15. Write a Shell script for converting the temperature from Fahrenheit to Celsius
fahrenheit_to_celsius() {
fahrenheit=$1
celsius=$(echo "scale=2; ($fahrenheit - 32) * 5 / 9" | bc)
echo $celsius
}
echo "Enter temperature in Fahrenheit:"
read fahrenheit
celsius=$(fahrenheit_to_celsius $fahrenheit)
echo "Temperature in Celsius: $celsius"
16. Develop a Shell program to find whether given number is perfect squareor
Not
clear
echo -n "Enter the Number==> "
read n
v=0
i=1
x=0
while test $i -le $n
do
x=`expr $i \* $i`
if test "$x" = "$n"
then
v=1
echo "The Number is a perfect square"
fi
i=`expr $i + 1`
done
if test $v -eq 0
then
echo "The Number is not a perfect square"
fi