LinuxQuestions.org
Review your favorite Linux distribution.
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, 06:37 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
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
 
Old 03-13-2013, 07:22 AM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
check out getopts you might like that.
 
Old 03-13-2013, 07:53 AM   #3
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by lleb View Post
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?
 
Old 03-13-2013, 10:00 AM   #4
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85

Original Poster
Blog Entries: 1

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


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
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
How do I make a template char* or wchar_t* default function argument binarybob0001 Programming 2 10-30-2009 01:46 AM
Howto use an argument in a function in C? daYz Programming 10 03-08-2007 03:22 AM
Function Pointer as an Argument in C trutnev Programming 5 05-24-2005 10:22 AM
C++ Function Argument gamehack Programming 1 09-13-2004 08:50 AM

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

All times are GMT -5. The time now is 02:20 AM.

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