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