Skip to content

Bash parameter parsing

Here is a code snippet I use for parameter parsing:

dohelp()
{
  "Example script"
  ""
 
  # Exit because you don't want the script to do anything after
  # displaying help, and do so with error, so that calling scripts won't think it succeeded
  1
}
 
  [ -n "$*" ];
  flag="$1"
  value="$2"
 
  "$flag"
    "--one")
      one="$value"
     
    ;;
    "--two")
      two="$value"
     
    ;;
    "--pretend")
      pretend=true
    ;;
    "--help")
      dohelp
    ;;
    "--")
     
    ;;
    *)
      -e "unknown option $flag\n"
      dohelp
    ;;
 
 


    No Comments ( Add comment / trackback )