Showing posts with label Shell Script Exercises. Show all posts
Showing posts with label Shell Script Exercises. Show all posts

Shell script to check Armstrong number

SCRIPT:
#!/bin/bash
echo -n "Enter the nuber : "
read read_val
number=$read_val
buf1=0
buf2=0
sum=0
while [ $number -gt 0 ]
do
    buf1=`expr $number % 10`
    buf2=`expr $buf1 \* $buf1 \* $buf1`
    sum=`expr $sum + $buf2`
    number=`expr $number / 10`
   
done
if [ $sum -eq $read_val ]
then
    echo "$read_val is armstrong number"
else
    echo "$read_val not a armstrong number"
fi
OUTPUT:
(i)
Enter the nuber : 178
178 not a armstrong number

(ii)
Enter the nuber : 178
178 not a armstrong number



   

Shell script to find factorial of an number

SCRIPT:
#!/bin/bash
input=$1
fact_op=1
while [ $input -gt 0 ]
do
    echo "$fact_op  $input"
    fact_op=$(( $fact_op * $input ))
    input=$(( $input - 1 ))
done
echo "factorial of value $1 is $fact_op"

OUTPUT:
sujin@sujin:~/work/shell$ ./factorial.sh 5
1  5
5  4
20  3
60  2
120  1
factorial of value 5 is 120

Shell script to check file executable permission availability

SCRIPT:
#!/bin/bash
USAGE="usage : $0 {filename}"
CHECK_FILE=$1
if [ $# -lt 1 ]
then
 echo "$USAGE"
 exit 1
fi
if [ -x "$CHECK_FILE" ]
then
 echo "$CHECK_FILE has the executable permission"
else
 echo "$CHECK_FILE not has executable permission"
fi
OUTPUT-1:
test.sh has the executable permission

OUTPUT-2:

sujin_sr not has executable permission

Shell Script to reverse string


SCRIPT
#!/bin/bash
input="$1"
output=""
output=$(echo $input | rev)
echo "Input  : $input"
echo "Output : $output"

OUTPUT:
sujin@sujin:~/sujin/common/script$ bash reverse.sh linux
Input  : linux
Output : xunil

Shell script to email CPU and MEMORY usage

DESCRIPTION:
Install sysstat and postfix before running the script

SCRIPT:

#!/bin/bash
#install below packeges befor running the script
#sudo apt-get install sysstat, sudo apt-get install postfix
SUB_CPU="CPU USAGE"
SUB_MEMORY="MEMORY USAGE"
TO=youradmin@anymail.com
CPU_LOG=/var/log/mylog
MEMORY_LOG=/tmp/mem_log
while ((1))
do
mpstat > $CPU_LOG
mail -s "$SUB_CPU" "$TO" < $CPU_LOG
sleep 2
cat $CPU_LOG
egrep --color 'Mem|Cache|Swap' /proc/meminfo > $MEMORY_LOG
mail -s "$SUB_MEMORY" "$TO" < $MEMORY_LOG
sleep 2
cat $MEMORY_LOG
sleep 60
done

OUTPUT:

sujin@sujin:~/sujin/work$ sudo ./test.sh
Linux 3.0.0-24-generic (sujin) 03/22/2013 _i686_ (4 CPU)

06:56:24 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle

06:56:24 PM  all    6.58    0.01    1.32    0.93    0.00    0.01    0.00    0.00   91.15
MemTotal:        3526272 kB
MemFree:         1495584 kB
Cached:          1072248 kB
SwapCached:            0 kB
SwapTotal:       4098044 kB
SwapFree:        4098044 kB
Linux 3.0.0-24-generic (sujin) 03/22/2013 _i686_ (4 CPU)

06:57:28 PM  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest   %idle

06:57:28 PM  all    6.55    0.01    1.32    0.93    0.00    0.01    0.00    0.00   91.18
MemTotal:        3526272 kB
MemFree:         1508548 kB
Cached:          1054596 kB
SwapCached:            0 kB
SwapTotal:       4098044 kB
SwapFree:        4098044 kB


Shell script to convert string lower to upper and upper to lower

SCRIPT:
#!/bin/bash
echo -n "enter any string in lowercase : "
read l_str
u_str=`echo $l_str | tr [a-z] [A-Z]`
echo "lower to upper converted o/p : $u_str"


echo -n "enter any string in uppercase : "
read u_str
l_str=`echo $u_str | tr [A-Z] [a-z]`
echo "upper to lower converted o/p : $l_str"

OUTPUT:
sujin@sujin:~/work/shell$ bash lwr2upr.sh
enter any string in lowercase : cricket
lower to upper converted o/p : CRICKET
enter any string in uppercase : FOOTBALL
upper to lower converted o/p : football



Shell script to find simple interest

SCRIPT:
#!/bin/bash
echo -n "enter principal : Rs."
read principal
echo -n "enter number of years : "
read year
echo -n "enter rate of interest : "
read interest
simple_interest=`expr "($principal * $year * $interest)/100"|bc`
echo "simple interest = Rs.$simple_interest"

OUTPUT:
sujin@sujin:~/work/shell$ bash SI.sh
enter principal : Rs.2000
enter number of years : 3
enter rate of interest : 8
simple interest = Rs.480
 
 

Shell script to cancatenate two string

SCRIPT:
#!/bin/bash
echo -n "Enter the 1st string : "
read str1
echo -n "Enter the 2nd string : "
read str2
cat_str="$str1$str2"
echo "cancatenated string : $cat_str"

OUTPUT:
sujin@sujin:~/work/shell$ bash str_concatenate.sh
Enter the 1st string : sujin
Enter the 2nd string : sr
cancatenated string : sujinsr
 

Shell script to find string length

SCRIPT:
#!/bin/bash
echo -n "enter the string : "
read str
length=`expr $str|wc -c`
length=`expr $length - 1`
echo "the length of the entered string $str is $length"

OUTPUT:
sujin@sujin:~/work/shell$ bash find_length.sh
enter the string : linux_ubuntu
the length of the entered string linux_ubuntu is 12


Shell script to ping IP

Here you can see the simple and easy way to check your PC able to ping particular IP.

SCRIPT:
#!/bin/bash
ping -c 3 $1 > /dev/null
if [[ $? -eq 0 ]]
then
    echo "ping success"
else
    echo "ping failed"
fi

OUTPUT:
i)
run -> ./ping.sh 192.168.1.4
ping success
 
ii)
run -> ./ping.sh 10.10.62.1 
ping failed
 

shell script to identify input entered or not

SCRIPT:
#!/bin/bash
echo -n "enter any value : "
read val
if [[ -n $val ]]
then
    echo "input entered"
else
    echo "not entered"
fi

OUTPUT:
i)run -> ./enter.sh
enter any value :
not entered
 
ii)run -> ./enter.sh
enter any value : hi
input entered
 

Shell script to check the file and directory availability

SCRIPT:
#!/bin/bash
DIR=/home/sujin/work/
FIL=/home/sujin/work/shell/if.sh

if [[ -d $DIR && ! -L $DIR ]]
then
    echo "directory $DIR available"
else    
    echo "directory $DIR not available"
fi

if [[ -f $FIL && ! -L $FIL ]]
then
    echo "file $FIL available"
else
    echo "file $FIL not available"
fi


OUTPUT:
run -> ./dir_file_avail.sh
directory /home/sujin/work/ available
file /home/sujin/work/shell/if.sh available


Shell script to find sum of two numbers

DESCRIPTION:
  • This script will read two integer value from users and give output as sum of two values
SCRIPT:

    #!/bin/bash
   
    echo -n "Enter the first number : "
    read num1
    echo -n "Enter the second number : "
    read num2
    sum=`expr $num1 + $num2`
    echo "sum of two value is $sum"

RUN: 

    sh sum.sh

OUTPUT:

   Enter the first number : 23
   Enter the second number : 45
   sum of two value is 68


Shell Script To Find The Area Of A Triangle

SCRIPT:

#!/bin/bash

#This script used to find the area
#of the triangle. user need to give
#the base and height of the triangle

echo -n "Please enter the base and height of the triangle : "
read base
read height
area=`expr "scale=2; 1/2*$base*$height"|bc`
echo "area of the triangle = $area"

RUN:

sh triangle.sh

OUTPUT:

Please enter the base and height of the triangle : 10
25
area of the triangle = 125.00