LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Entering no value intp getopts case statement? (https://www.linuxquestions.org/questions/linux-newbie-8/entering-no-value-intp-getopts-case-statement-4175453949/)

Batistuta_g_2000 03-13-2013 03:30 PM

Entering no value intp getopts case statement?
 
How can you have a default action for no-value in a case-getopts?

while getopts [ $# -lt 1 ]; do
not working - see end of code:


Code:

while getopts "b:nlapdth" OPTION; do
              case "$OPTION" in 
                  b)
                    case "${OPTARG}" in
                        full)
                            echo "LOGFLAG=false"
                            echo "FULL_LOGGING=true"
                            echo "logging_option"
                        ;;                       
                        error)
                          echo "ERRORLOGGING AS NORMAL" 
                        ;;
                  esac ;;
                             
                  n)
                            echo "restore_filename"
                            ;;                           
                  l)
                            echo "restore_latest"
                            ;;
                  a)
                            echo "listall"
                            ;;
                  p)
                            echo "project_timestamp"
                            ;; 
                  c) 
                            echo "last_contents"
                            ;;
                  d)
                            echo "RESTORE_FILES_DIRECTORY="   
                            ;;
                  t)
                            echo "Compare"
                            ;;
                  h)
                            echo " help_action"
                            ;;
                  ?)
                            echo "HELLP HELP"
                            ;; 
                  *)
                            echo "NO ENRTY"
                            ;;
esac


while getopts [ $# -lt 1 ]; do
                  echo -e "Please enter an option"
  echo "Usage `basename $0` for options (-b|-n|-l|-a|-p|-d|-t|-h)"
  echo "Please specify at least one of the optione listed above."
 exit
done   
done
+ getopts b:nlapdth OPTION


Batistuta_g_2000 03-13-2013 03:55 PM

Quote:

Originally Posted by Batistuta_g_2000 (Post 4910983)
How can you have a default action for no-value in a case-getopts?

while getopts [ $# -lt 1 ]; do
not working - see end of code:


Code:

while getopts "b:nlapdth" OPTION; do
              case "$OPTION" in 
                  b)
                    case "${OPTARG}" in
                        full)
                            echo "LOGFLAG=false"
                            echo "FULL_LOGGING=true"
                            echo "logging_option"
                        ;;                       
                        error)
                          echo "ERRORLOGGING AS NORMAL" 
                        ;;
                  esac ;;
                             
                  n)
                            echo "restore_filename"
                            ;;                           
                  l)
                            echo "restore_latest"
                            ;;
                  a)
                            echo "listall"
                            ;;
                  p)
                            echo "project_timestamp"
                            ;; 
                  c) 
                            echo "last_contents"
                            ;;
                  d)
                            echo "RESTORE_FILES_DIRECTORY="   
                            ;;
                  t)
                            echo "Compare"
                            ;;
                  h)
                            echo " help_action"
                            ;;
                  ?)
                            echo "HELLP HELP"
                            ;; 
                  *)
                            echo "NO ENRTY"
                            ;;
esac


while getopts [ $# -lt 1 ]; do
                  echo -e "Please enter an option"
  echo "Usage `basename $0` for options (-b|-n|-l|-a|-p|-d|-t|-h)"
  echo "Please specify at least one of the optione listed above."
 exit
done   
done
+ getopts b:nlapdth OPTION


Got easy answer but not he best one probably:

if [ -z "$*" ]; then echo "No args";
echo -e "Please enter an option"
echo "Usage `basename $0` for options (-b|-n|-l|-a|-p|-d|-t|-h)"
echo "Please specify at least one of the optione listed above."
fi

while getopts "b:nlapdth" OPTION; do


All times are GMT -5. The time now is 08:39 PM.