Bash Shell Scripting
Crash Course
crowfly.net

<<   >>

Introduction

GNU_Tools

Example1

Example2

Execution_bit

Bang_bin_bash

Example3

The_dot

Example4

Example5

Example6

Example7

Example8

Background

Forking

Example9

Bash_profile

Pattern_match

Regex

Debug

Gory_details

References

<<   >>
Example 3

Here is a real script run every night in a cron jobs, check_filesystem_highwater_mark:

  #!/bin/bash

  # check filesystem high water mark
  df -m | \
    grep -ve Filesystem -e cdrom -e VideoProject -e '^ ' | \
    sed "s/%//" | \
    # Hardcoding HIGHWATER mark here:
    awk '$5 >= 80 { print "\t" $6 "\t" $1 "\t" $5 "% Capacity."}'

The output looks like this:

  dlink@viddev1> ./check_filesystem_highwater_mark 
          /scan2k/r2d2    SCAN2K/DLINK    88% Capacity.

Explanation:

1.) Let's break down all the pieces, the first command is df, (disk filesystem)

  dlink@viddev1> df -m
  Filesystem           1M-blocks      Used Available Use% Mounted on
  /dev/sda1                 1028       720       309  71% /
  tmpfs                     2027         1      2027   1% /dev/shm
  /dev/sda10              868551    589700    278851  68% /data
  /dev/sda8                20481      3708     16774  19% /home
  /dev/sda9                 1028       722       306  71% /tmp
  /dev/sda5                20481      2212     18270  11% /usr
  /dev/sda7                 2056      1133       923  56% /var
  SCAN2K/DLINK            550424    481992     68433  88% /scan2k/r2d2
  DUDLY/DLINK             270395     81393    189002  31% /dudly/do1
  //halfpint/videoproject
                         1669312   1504352    164960  91% /netbackup

2.) Next we grep to filter the output:

  dlink@viddev1> df -m | grep -ve Filesystem -e cdrom -e VideoProject 
  -e '^ '
  /dev/sda1                 1028       720       309  71% /
  tmpfs                     2027         1      2027   1% /dev/shm
  /dev/sda10              868551    589700    278851  68% /data
  /dev/sda8                20481      3708     16774  19% /home
  /dev/sda9                 1028       722       306  71% /tmp
  /dev/sda5                20481      2212     18270  11% /usr
  /dev/sda7                 2056      1133       923  56% /var
  SCAN2K/DLINK            550424    481992     68433  88% /scan2k/r2d2
  DUDLY/DLINK             270395     81393    189002  31% /dudly/do1

3.) Then we ixnay the percent symbol in order to be able read the percent column as a number:

  dlink@viddev1> df -m | grep -ve Filesystem -e cdrom -e VideoProject 
  -e '^ ' | sed "s/%//" 
  /dev/sda1                 1028       720       309  71 /
  tmpfs                     2027         1      2027   1 /dev/shm
  /dev/sda10              868551    589700    278851  68 /data
  /dev/sda8                20481      3708     16774  19 /home
  /dev/sda9                 1028       722       306  71 /tmp
  /dev/sda5                20481      2212     18270  11 /usr
  /dev/sda7                 2056      1133       923  56 /var
  SCAN2K/DLINK            550424    481992     68433  88 /scan2k/r2d2
  DUDLY/DLINK             270395     81393    189002  31 /dudly/do1

4.) Finally we use awk to get only those rows where the 5th column is >= 80:

  dlink@viddev1> df -m | grep -ve Filesystem -e cdrom -e VideoProject 
  -e '^ ' | sed "s/%//" | awk '$5 >= 80 { print "\t" $6 "\t" $1 "\t"
  $5 "% Capacity."}'
          /scan2k/r2d2    SCAN2K/DLINK    88% Capacity.

5.) Then we

  • cut and paste the command that we painstakingly crafted into a file,
  • place a bunch of (escaped) carriage returns for clarity and maintainability,
  • set the execution bit,
  • and we're done.

  dlink@viddev1> chmod 775 check_filesystem_highwater_mark
  dlink@viddev1> ls -l check_filesystem_highwater_mark
  -rwxrwxr-x  1 dlink viddev 235 2006-07-20 12:21 check_filesystem_highwater_mark
  dlink@viddev1> ./check_filesystem_highwater_mark 
          /scan2k/r2d2    SCAN2K/DLINK    88% Capacity.