LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   GETOPTS in CASE statement (https://www.linuxquestions.org/questions/linux-newbie-8/getopts-in-case-statement-4175453883/)

Batistuta_g_2000 03-13-2013 05:13 AM

GETOPTS in CASE statement
 
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?

[/code]

#!/bin/bash
set -xv
#######################################
# CASE test script #
#######################################

while getopts "br:aeh" OPTION; do
case "$OPTION" in

b)
echo "backup_action"
;;

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;;
f)
echo "LOGFLAG=false"
echo "FULL_LOGGING=true"
echo "logging_option"
;;
e)
echo "LOGFLAG=false"
echo "FULL_LOGGING=false"
echo "logging_option"
;;
h)
echo " help_action"
;;
esac
done ########## WAS MISSING!!!!
[code]

Is this the best way to go?
Also how would I offer only one option as in backup or restore and fulllogging or errorlogging?

jlinkels 03-13-2013 10:09 PM

Writing functions for each task is a correct approach.

You can save yourself the coding of the getopts. Use shflags.

http://code.google.com/p/shflags/wiki/Documentation10x

jlinkels


All times are GMT -5. The time now is 06:07 PM.