Bash Template



  • I posted another version of this in another thread, but I figured I'd post a more complete version here. This template handles locking and unlocking of the script and also passing of arguments. It also includes a main function and a trap for specific signals.

    #!/bin/bash
    
    #MIT License
    
    #Copyright (c) 2018  John Hooks
    
    #Permission is hereby granted, free of charge, to any person obtaining a copy
    #of this software and associated documentation files (the "Software"), to deal
    #in the Software without restriction, including without limitation the rights
    #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    #copies of the Software, and to permit persons to whom the Software is
    #furnished to do so, subject to the following conditions:
    
    #The above copyright notice and this permission notice shall be included in all
    #copies or substantial portions of the Software.
    
    #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    #SOFTWARE.
    
    
    
    readonly NAME=$(basename "$0")
    readonly LOCKFILE=/tmp/$NAME
    readonly LOCKFD=200
    
    #Remove lock file if signal 1, 2, 3, or 6.
    trap prem_end 1 2 3 6
    
    prem_end ()
    {
      echo "Premature end detected, removing lock"
      rm -rf $LOCKFILE.lock
      echo "Finished"
      exit 1
    }
    
    #Lock functions
    lock () {
        local fd=${2:-$LOCKFD}
        local lockfile=$LOCKFILE.lock
    
        # create the lock file
        eval "exec $fd>$lockfile"
    
        #get the lock
        flock -n $fd && return 0 || return 1
    }
    
    unlock () {
        local fd=${2:-$LOCKFD}
        local lockfile=$LOCKFILE.lock
    
        #unlock
        flock -u $fd
    
        #remove lock file
        rm -f $lockfile
    }
    
    errexit () {
        local err="[email protected]"
    
        echo $err
        exit 1
    }
    
    
    #Help function
    function script_help () {
         echo "
             Usage: $(basename $0) [options] 
       
                 -h   this help text
       
             Example:
               $(basename $0) "
       
         exit ${1:-0}
    }
    
    
    #Show help if no arguments or options are passed
    [[ ! "$*" ]] && script_help 1
    OPTIND=1
    
    
    #Read command line options
    #A colon after a flag means it takes an argument
    #Example with extra argument called "a"
    #while getopts "a:h" opt; do
    #    case "$opt" in
    #      a) variable=$OPTARG ;;
    #      h) script_help ;;
    #      \?) script_help 1 ;;
    #    esac
    #done
    while getopts "h" opt; do
        case "$opt" in
          h) script_help ;;
          \?) script_help 1 ;;
        esac
    done
    shift $(($OPTIND-1));
    
    #Main function
    main () {
        lock $NAME || errexit "An instance of $NAME is still running."
    
    
    
    
    
        unlock
    }
    main
    

Log in to reply