help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

getopts silent error reporting


From: uzibalqa
Subject: getopts silent error reporting
Date: Sat, 06 May 2023 09:25:18 +0000

Is it possible to have a function option that selects whether to use silent 
error reporting
for getopts or not?

kdump ()
{
 impl=""
 if [[ "$impl"  == "SILENT" ]]; then
   opstring=":n:z:"  # Short options string
 else
   opstring="n:z:"
 fi

 while getopts "$opstring" opname; do
  case ${opname} in
    ("n")
      if [ -n "$OPTARG" ]; then
        echo "The string is not empty"
      else
        echo "The string is empty"
      fi
      ;;
    ("z")
      if [ -z "$OPTARG" ]; then
        echo "The string is empty"
      else
        echo "The string is not empty"
      fi
      ;;
    (\?)
      ## Invalid Option Found, OPNAME set to '?'
      echo "Invalid option: -$OPTARG" 1>&2
      exit 1
      ;;
    (:)
      ## Required option argument not found, OPNAME set to ':'
      echo "Option -$OPTARG requires an argument" 1>&2
      exit 1
      ;;
  esac
 done
}




reply via email to

[Prev in Thread] Current Thread [Next in Thread]