Question

I have a file parsing option flags from command line input, and if you pass in -h, it's giving an error.

h)  
    usage
    exit 1
;;

returns: usage: command not found

Does anybody know what’s going on here? I've found many examples that use usage, so I would think it should work. I can’t seem to find any relevant info on it searching google.

Was it helpful?

Solution 2

What you are missing is a function definition of usage:

function usage {
    cat <<-USAGE
         Now you can define your usage here
         take as many lines as you want. When
         you finish, just put "USAGE" on a line
         by itself.
    USAGE
}

Put this BEFORE the call of usage. Remember that the line with USAGE must be preceded only by tabs.

OTHER TIPS

You need add to your shell script before you call the usage the next:

usage() {
    echo  "$0: some help text"
}

Those working examples define a function called usage in their source.

As you see, it gets called, when you provide the -h flag.

The usual reason behind moving it to a function is that

  • it doesn't clutter the code
  • can be reused, e.g. when you provide invalid arguments.

usage is not a command, but the common name for a custom shell function you supply that outputs usage information for your script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top