ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Bash Template

    IT Discussion
    1
    1
    211
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • stacksofplatesS
      stacksofplates
      last edited by stacksofplates

      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="$@"
      
          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
      
      1 Reply Last reply Reply Quote 4
      • 1 / 1
      • First post
        Last post