Bash Scripting

Getting Started

    • Always begin your bash script with:

#! /bin/bash

# comments

This is the header line. It declares the interpreter you will use to run your script. You may add options at the end of this line.

A good example is:

#! /bin/bash -xv

This specifies to run the script in verbose mode with execution tracing turned on.

    • Describe what your program is with comment lines, for example:

#! /bin/bash

# Name: diskuse

# Author: Thomas Sandholm (mailto:tom.sandholm@gmail.com)

# Date: Thu Aug 21 16:05:23 EDT 2008

# Version: 1.0

# Summary: program to display local disk usage

# Description: This program will display the local disk usage.

# It uses the df command but filters the output to put

# it in a form that's easier to parse by other programs.

    • Declare your global variables and functions, for example:

# get the name of this program

PNAME=$(basename $0)

# Error handler

function Die {

echo "$*"

exit 1

}

    • Put in your Main section, for example:

# Section: MAIN

# We run the df command, with options to list only local filesystems of type ext3.

# We strip out the header line, and remove multiple whitespace and replace

# it with a colon (:), printing results to stdout. If we get any errors

# we call the function Die with some error message.

df -l -t ext3 | sed -e 1d -e 's/[ ][ ]*/:/g' || Die "${PNAME}: failed to run"

    • And close your program. Of course the exit is redundant, but it's good practice. This way the next person to read your script will know for sure that this is what you defined to be the end of the program.

# all done

exit 0

    • So here's the completed program:

#! /bin/bash

# Name: diskuse

# Author: Thomas Sandholm (mailto:tom.sandholm@gmail.com)

# Date: Thu Aug 21 16:05:23 EDT 2008

# Version: 1.0

# Summary: program to display local disk usage

# Description: This program will display the local disk usage.

# It uses the df command but filters the output to put

# it in a form that's easier to parse by other programs.

# get the name of this program

PNAME=$(basename $0)

# Error handler

function Die {

echo "$*"

exit 1

}

# Section: MAIN

# We run the df command, with options to list only local filesystems of type ext3.

# We strip out the header line, and remove multiple whitespace and replace it

# with a colon (:), printing results to stdout. If we get any errors

# we call the function Die with some error message.

df -l -t ext3 | sed -e 1d -e 's/[ ][ ]*/:/g' || Die "${PNAME}: failed to run"

# all done

exit 0

    • Now put a copy of your program in a place where it can either be found from your PATH, and set execute permissions on it.

chmod +rx ./diskuse

    • And run it:

[sandholm@sys1-lnx ~]$ ./diskuse

/dev/sda3:6048352:3612692:2128420:63%:/

/dev/sda1:10080488:4731368:4837052:50%:/boot

/dev/sda9:5036284:4356924:423528:92%:/home_local

/dev/sda7:8064272:3046256:4608364:40%:/opt

/dev/sda6:10080488:2783044:6785376:30%:/prod

/dev/sda10:5036284:43268:4737184:1%:/tmp

/dev/sda5:10080488:2447224:7121196:26%:/usr

/dev/sda8:8064272:1106252:6548368:15%:/var

[sandholm@sys1-lnx ~]$

Check out my article on creating network services for information on how to make this diskuse script run as a network service, Create a Network Service.