Sample Programs - LFD
Sample Programs - LFD
#!/bin/bash
echo "Please enter some text:"
read user_input
echo "You entered: $user_input"
stored_input=$user_input
echo "Stored input: $stored_input"
num1=10
num2=20
sum=$(($num1+$num2))
echo "The Sum is: $sum"
if [ $op == + ]
then
echo "$num1 + $num2 = $((num1+num2))"
elif [ $o == - ]
then
echo "$num1 - $num2 = $((num1-num2))"
elif [ $op == * ]
then
echo "$num1 * $num2 = $((num1*num2))"
elif [ $op == / ]
then
echo "$num1 / $num2 = $((num1/num2))”
else
echo "Operator not listed"
fi
case $op in
and)
if [[ $val1 == true && $val2 == true ]]
then
echo "Result: true"
else
echo "Result: false"
fi;;
or)
if [[ $val1 == true || $val2 == true ]]
then
echo "Result: true"
else
echo "Result: false"
fi;;
not)
if [[ $val1 == true ]]
then
echo "Result: false"
else
echo "Result: true"
fi;;
*) echo "Invalid operator."
Esac
8. To check if a given email is valid or not
#!/bin/bash
a) String length
#!/bin/bash
Palindrome () {
s=$1
if [ "$(echo $s | rev)" == "$str" ]
then
echo "The string is a Palindrome"
else
echo "The string is not a palindrome"
fi
}
read -p "Enter a string: " str
Palindrome "$str"
Prime () {
num=$1
if [ $num -lt 2 ]
then
echo "The number $num is Not Prime"
return
fi
for (( i=2; i<=$num/2; i++ ))
do
if [ $((num%i)) -eq 0 ]
then
echo "The number $num is Not Prime"
return
fi
done
echo "The number $num is Prime"
}
read -p "Enter a number: " num
Prime "$num"
Celsius () {
f=$1
c=$((($f-32)*5/9))
echo "Temperature in Celsius = $c°C"
}
Area() {
width=$1
height=$2
area=$(($width * $height))
echo “Area of the rectangle is: $area”
}
arr=(24 27 84 11 99)
arr=(24 27 84 11 99)
Descriptive