LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-13-2013, 05:13 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
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?

Last edited by Batistuta_g_2000; 03-13-2013 at 05:39 AM.
 
Old 03-13-2013, 10:09 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] GETOPTS in CASE statement Batistuta_g_2000 Linux - Newbie 1 03-04-2013 05:12 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
Case Statement craigjward Programming 6 12-12-2007 11:33 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 08:05 PM
case statement baks Programming 2 03-15-2007 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration