7 Comments

  1. max

    best explanation I've seen so far. Thank you so much.

    Reply
  2. manish

    is there a variable in getopts which stores the error message.
    while getopts "a:" opt
    do
    case $opt in
    a) echo "found a";;
    *) echo "invaid parameter"
    esac
    done
    echo "code successfully executed"

    I want that when i run this code without passing parameter to -a, the code should stop executing and the last line should not be printed out.
    Is there a way?

    Reply
    • AW

      Yes there is:
      add
      SNIPPET---
      🙂
      echo "Option -$OPTARG requires an argument."
      exit 1
      ;;
      END---
      before (!) the *) option

      Reply
  3. thanks

    i recently discovered bash had getopts -- I always used the tried and true method of SHIFT ;
    I will have to borrow you sample for my next bash tool.

    Reply

  4. Hi,
    Thanks very much for this blog.
    Really helpful.

    Reply
  5. Ben Patridge

    Thank you for this. It was extremely helpful!

    Reply
  6. Dan Saint-Andre

    Another aspect of command-line option processing involves what I call "qualifiers." When building an application, some options might be required. Other options might be completely, well, optional. Still other options might require one or more different options to clarify or modify the behavior of the primary option. Some options might be mutually exclusive. I think that you get the idea.
    While we can implement rules and program logic to enforce these "qualifiers," it might be nice to have something in a table or similar so that our application need not have pages of ad hoc code to do the work.

    Regards,
    ~~~ 0;-Dan

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.