Bash: Command line parser for Multiple arguments to bash

By Eric Downing. Filed in Uncategorized  |  
TOP del.icio.us digg

I regularly have to write scripts into different environments and when I am wrapping a build system, I typically use a Bash script to do it. I often have to pass a set of arguments to the underlying commands in my scripts as well. I have found that the %* argument is the easiest way to take those arguments.

The %* takes the arguements and passes them along. The %1, %2, %3…and so on are the individual arguments but unless you have a fixed amount of arguments, then you will need to process a variable amount of arguments.

while [ $#  -gt 1 ]
do
  case "$1" in
    -a)
      ANALYZE=1
      ;;
    *)
     echo "Invalid option"
     ;;
   esac
   shift
done

Leave a Reply