Sample Shell Scripting Exercises for Beginners

Learning  coding isn’t simple . We need to do lot of practice than reading . So in order to practice we need some exercises . Here are few examples where you can try to built a code on your own. These examples are for only begineers

1. Shell Script to find given string is Armstrong or not

#!/bin/bash
#This Script will check the given number is Armstrong or not (3 digit numbers)
##########################*************************##############################
echo "You are going to test a Number Armstrong number or not"
echo "Please enter the Number and hit Enter"
read num1
sum=0
num=$num1
count=`echo $num1 | wc -c`
             if [ $count -eq 4 ];then
                    case $num1 in
                    [0-9][0-9][0-9])
                           while [ $num -gt 0 ]
                           do
                                 n1=`expr $num % 10`
                                 cube=`expr $n1 \* $n1 \* $n1`
                                 sum=`expr $sum + $cube`
                                 num=`expr $num / 10`
                           done
              if [ "$sum" -eq "$num1" ];then
                     echo "Entered Number $num1 is a ARMSTRONG number"
              else
                     echo "Entered Number $num1 is not an ARMSTRONG number"
               fi
                     echo "The cubes of the given Number is $sum"
                       ;;
                      *) echo "enter only digits"
                       ;;
                      esac
                else
                    echo "Enter only Numeric Digits"
               fi

2. Shell Script to find the factorial of a given Number

#!/bin/sh
#This script will find the factorial of any digit
######################************************#####################
num=0
num1=0
fact=1 
echo -n "Enter number to find factorial : "
read num
num1=$num
      while [ $n -ge 1 ]
      do
            fact=`expr $fact \* $num`
            num=`expr $num - 1`
       done
echo "Factorial for $num1 is $fact"

3. Shell Script to send the report of CPU usage and Memory Usage to Email Address

#!/bin/bash
#This Script will send the report of CPU/Memory Usage to specified Email address

echo -e "Below is the CPU usage Report\n" > UsageReport_cpu_memory
echo -e "*******************************************\n" >> UsageReport_cpu_memory
top -n 1 | head -5 >> UsageReport_cpu_memory
echo -e "********************************************\n" >> UsageReport_cpu_memory
echo -e "Below is the Memory Usage\n"
echo -e "****************************************\n" >> UsageReport_cpu_memory
free >> UsageReport_cpu_memory

cat UsageReport_cpu_memory
Advertisements

Express your Opinion here

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s