Essential Shell Scripting Examples for Linux Automation
1. Armstrong Number Checker
Write a shell script to check whether a given number is an Armstrong number.
echo "Enter a number"
read c
x=$c
sum=0
while [ $x -gt 0 ]
do
r=$(( $x % 10 ))
n=$(( $r * $r * $r ))
sum=$(( $sum + $n ))
x=$(( $x / 10 ))
done
if [ $sum -eq $c ]
then
echo "This is an Armstrong number"
else
echo "This is not an Armstrong number"
fi2. Calculate Area of a Circle
Write a shell script to find the area of a circle.
echo "Enter radius"
read r
echo "Area is"
echo "3.14 * $r * $r" | bc3. Average of Command Line Arguments
Write a shell script to find the average of numbers entered as command line arguments.
sum=0
for i in $*
do
sum=$((sum + i))
done
echo "Summation of $# numbers is: $sum"
N=$#
avg=$(echo "$sum / $N" | bc -l)
echo "Average = $avg"... Continue reading "Essential Shell Scripting Examples for Linux Automation" »
English with a size of 3.37 KB