Search This Blog

Tuesday, December 3, 2013

Basic Shell Scripting - Examples


Example 1:  Sum the file size.


Suppose there is a file named “dir_list” containing lines like :

-rwxr-xr-x   1 test  deba           3139 Apr 29 2013  BINARY_LIST*
-rw-r--r--   1 test  deba           4078 Apr 29 2013  BINARY_CKSUM_002
-rw-r--r--   1 test  deba           5343 Apr 29 2013  CONFIG_CKSUM_002
-rw-r--r--   1 test  deba           4078 Apr 29 2013  BINARY_CKSUM_03
-rw-r--r--   1 test  deba           5288 Apr 29 2013  CONFIG_CKSUM_03
-rw-r--r--   1 test  deba             70 Apr 29 2013  BINARY_DIFF_002_03
-rw-r--r--   1 test  deba            439 Apr 29 2013  CONFIG_DIFF_002_03
-rwxr-xr-x   3 test  deba            512 Oct  8 15:48 TOKENIZER/
-rwxr-xr-x   5 test  deba            512 Nov 27 09:57 TEST/
-rw-r--r--   1 test  deba              0 Dec  3 19:44 dir_list


Suppose you want to sum the size of the files :


awk '{a+=$5} END{print "Total",a}' dir_list


o/p: Total 23459

"a" is a variable.
$5 is the 5th column

Example 2: Check directory usage and send email.


Suppose you want to write a small script to check the directory size of “var” and if it is more than 60% raise an alert   -

Current State :

a) Check with df -k

Filesystem    1024-blocks      Free %Used    Iused %Iused Mounted on
/dev/hd4            65536     24052   64%     3116    10% /
/dev/hd2          3604480    574392   85%    65807     8% /usr
/dev/hd9var       1376256    552436   60%    19949     6% /var
/dev/hd3           983040    177612   82%     7661     4% /tmp
/dev/hd1          2031616     42080   98%    35577     8% /home
/dev/lv00          393216    298668   25%       43     1% /usr/sys/inst.images
/dev/lv01          851968    154724   82%    11666     6% /opt
/dev/igwlv        5111808   2025048   61%    30169     3% /igw
/dev/lv04         1048576    202256   81%    23799    10% /var_app_temp
/dev/lv02         2621440    396772   85%     4757     1% /opt/webMethods
/proc                   -         -    -         -     -  /proc
/dev/lv06         8388608   7015624   17%    33083     2% /var/app
/dev/lv07         1048576    575392   46%     1535     1% /var/app/temp
/dev/lv05         2097152    452448   79%   170674    33% /work
/dev/lv08          393216     53312   87%     6476     7% /home_d01
/dev/syduigd03_lv       65536     47820   28%     1911    16% /syduigd03
/dev/sh_MQ         458752    243400   47%     1731     2% /MQHA
/dev/sh_app       8388608   1320016   85%    26777     2% /var_app2
/dev/sh_app_temp     1572864    615364   61%    22202     6% /var_app_temp2

b) Filter out the "var/" using grep.

df -k|grep "/var"

c) Remove the directory which you do not want :

df -k|grep "/var"|grep -v "_"|grep -v "app"

d) Take just the 4th column containing size :

df -k|grep "/var"|grep -v "_"|grep -v "app"|awk '{print $4}'

e) Remove the "%" sign

df -k|grep "/var"|grep -v "_"|grep -v "app"|awk '{print $4}'|tr -d %

f) Store it in a variable :

VAR_USAGE=`df -k|grep "/var"|grep -v "_"|grep -v "app"|awk '{print $4}'|tr -d %`

g)Check the size :

echo $VAR_USAGE

h) send an email :

if [ "$VAR_USAGE" -gt "85" ] ; 
then echo "USAGE HIGH" | mailx -s "VAR_USAGE in $SERVER_NAME is $VAR_USAGE%" \ <email id> fi