when i run below code with option p and no parameters, it is going to other options where as it should execute the print statement of case p ). what needs to be fixed

Also, i want the code to execute h|* ) block when no options are given. How to achieve it.


while getopts ":vhp:" Option; do
  case ${Option} in
    v )
      print -- "${ScriptVersion}" 2>/dev/null
      exit 0

    p )
      if [[ -z ${AbsPackageName} ]]; then
        print -- "Missing Information.Please provide Package Name details."
        exit 2

    h|* )
      print -- "USAGE: ${ScriptName} -p [Absolute path of Package Name]" 2>/dev/null
      print -- "Parameter Descriptions:" 2>/dev/null
      print -- "-v ) Script Version" 2>/dev/null
      print -- "-h ) Show this usage message" 2>/dev/null
      exit 0



[root@localhost Desktop]# ./pkg2.ksh -p
USAGE: pkg2.ksh -p [Absolute path of Package Name]
Parameter Descriptions:
-v ) Script Version
-h ) Show this usage message

1 Answer 1


You should use h|* ) as the last case block, because it matches anything before checking -p.

  • Thats right. I modified the above code as suggested, but getting same error when i run with option p and with out parameters. Oct 23, 2017 at 10:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .