LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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-14-2013, 09:53 AM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Case statement with two option arguments?


Hi I need to make a case statement which takes two arguments:

./backup_restore -r myfile.tar.gz -d new directory



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

while getopts "b:r:" OPTION; do
               case "$OPTION" in  
                       
                 b)    
               case "${OPTARG}" in                
                        full)
                            echo "FULL_LOGGING"
                        ;;                         
                        error)
                           echo "ERRORLOGGING"
                        ;;
                            *) 
                           echo "Wrong choice"
                       ;;   
                      esac ;;
                              
                 r)
                          while getopts "d:" OPTION2; do
                           case "$OPTION2" in 
                       d)
                             echo "restore_filename "$OPTION1" to dir "$OPTION2"
                             ;;                             
                 
                       *)
                         echo "NOT A VALID OPTION"     
                         ;;
                    esac;;
                 done 
 esac

done
can I do a while getopts case in a while getopts case? and how are the arguments passed to the function?

I need to write a function that takes both $1 and $2 arguments and uses them as tar.gz filename and $2 as new_restore_directory.

Help....
 
Old 03-14-2013, 01:44 PM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,630

Rep: Reputation: 495Reputation: 495Reputation: 495Reputation: 495Reputation: 495
Code:
### Options
###################################

while getopts ":nx" opt
do
	case $opt in
		n )	NO_KILL=true
			;;
		x ) 	set -xvv
			;;
	esac
done

shift $(( $OPTIND -1 ))

### lftp function
###################################
lftp1()

{
if [ "${NO_KILL}" != "true" ]
then
	lftp -e "set ftp:passive-mode on && cd ${backdir1} && put ${NEWTAR} && bye" -u ${username},${password} ${url1}
else
	lftp -e "set ftp:passive-mode on && cd ${backdir1} && put /tmp/${tarfilename}.tar.bz2 && bye" -u ${username},${password} ${url1}
fi

}

### Not used in NO_KILL mode

if [ "${NO_KILL}" != "true" ]
then
	killProcs
fi
etc... once you create the getopts options before you make the call to the function, just run a simple test.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] arguments if statement brownie_cookie Linux - Newbie 4 05-03-2011 05:59 AM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
[SOLVED] case statement passing multiple arguments supulton Programming 2 08-07-2010 09:00 PM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 09:05 PM
case statement baks Programming 2 03-15-2007 02:19 PM

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

All times are GMT -5. The time now is 03:39 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration