0

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.

Code:

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

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

    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
    ;;

  esac
done

Execution:

[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
2

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. – Su_scriptingbee Oct 23 '17 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.