LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   case function for no-argument - default function for script? (https://www.linuxquestions.org/questions/linux-newbie-8/case-function-for-no-argument-default-function-for-script-4175453896/)

Batistuta_g_2000 03-13-2013 06:37 AM

case function for no-argument - default function for script?
 
I need to write a one script which does a number functions with these options in any order.

backup *default file to be restored (no options)
backup *change default config file location
backup *errorlogging only
backup *fullerrorlogging

restore *default last backup restored
restore *restore by filename
restore *list all backups
restore *list backups by name
restore *list contents of last backup
restore *change default restore location to new one
restore *carry out diff on tar contents

My solution is to write functions for each task in script but how should I parse the options to the script - was thinking of the case within a case below - is this the best way to go - will I have problems? is there an easier way?

But main question is how do you run a case with no options - as in a default action for the script below -

z={(( $# )) || printf '%s\n' 'No arguments'}
echo "$z"

Then use "$z" as an option??? would this work?


Code:


#!/bin/bash
# set -xv
######################################################
#        Setting up variables for script            #
######################################################

z={(( $# )) || printf '%s\n' 'No arguments'}
echo "$z"

while getopts "b:r:h" OPTION; do
              case "$OPTION" in 
                     
                  b)
#                if [[ ! $OPTARG ]]; then echo "No option."
#                else
                  case "${OPTARG}" in
                                                                                     
                        f)
                            echo "LOGFLAG=false"
                            echo "FULL_LOGGING=true"
                            echo "logging_option"
                          # [[ ! $OPTARG ]] && echo "No option."
                        ;;                       
                        e)
                          echo "ERRORLOGGING AS NORMAL" 
                        ;;
                  esac;;
                 
                  r)
                  case "${OPTARG}" in
                             
                      name)
                            echo "restore_filename"
                            ;;                           
                    latest)
                            echo "restore_latest"
                            ;;
                    listall)
                            echo "listall"
                            ;;
                    project)
                            echo "project_timestamp"
                            ;; 
              lastcontents) 
                            echo "last_contents"
                            ;;
                  directory)
                            echo "RESTORE_FILES_DIRECTORY="   
                            ;;
                    compare)
                            echo "Compare"
                            ;;
                  esac;;     
                         
          h)
            echo " help_action"
          ;;
        ?)
            echo "HELLP HELP"
          ;; 
        "$z")
          echo "NO ENRTY"
        ;;
   
esac


lleb 03-13-2013 07:22 AM

check out getopts you might like that.

Batistuta_g_2000 03-13-2013 07:53 AM

Quote:

Originally Posted by lleb (Post 4910679)
check out getopts you might like that.

I have getopts but how to pass arguments to script functions? and in what order is they can be added to case in any order how do you know which $# to parse to which function?

Batistuta_g_2000 03-13-2013 10:00 AM

Help?!!
 
How can I set the case to pick either b or r and not both?

[/code]

#!/bin/bash
# set -xv
######################################################
# Setting up variables for script #
######################################################

while getopts "b:r:h" OPTION; do
case "$OPTION" in
b)
while getopts "f:e:" OPTION2; do
case "$OPTION2" in
f)
echo "FULL_LOGGING=true"
;;
e)
echo "ERRORLOGGING AS NORMAL"
;;
esac
done;;
r)
case "${OPTARG}" in

name)
echo "restore_filename"
;;
latest)
echo "restore_latest"
;;
listall)
echo "listall"
;;
project)
echo "project_timestamp"
;;
lastcontents)
echo "last_contents"
;;
directory)
echo "RESTORE_FILES_DIRECTORY="
;;
compare)
echo "Compare"
;;
esac;;

h)
echo " help_action"
;;
?)
echo "HELLP HELP"
;;
*)
echo "NO ENRTY"
;;

esac
done
[code]


All times are GMT -5. The time now is 10:15 AM.