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-19-2013, 12:51 PM   #1
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Rep: Reputation: Disabled
Simple backup/restore Case menu:


Simple backup/restore Case menu:

Will not work - I need options
-b -full
-b -error
or
-r -n xxxx xxxx xxxx xxx (as many as you like)
-r xxxxx -d xxxxxx (restore name and new directory)

Have the seconf -r xxxx -d xxxx working.........
..................................................
BUT, I cannot seem to find any way of getting the
-r -n [Array] to load into case below.

Please help, stuck for days now.


Code:
clear

while getopts "b:r:c:" OPTION; do
               case "$OPTION" in                        
                 b)
                     case "${OPTARG}" in 
                                                                                      
                        full)
                          backup_action 
                        ;;                         
                        error)
                          backup_action 
                        ;;
                      *) 
                           echo "Wrong choice"
                       ;;   
                      esac ;;                             
                 r)
                     while getopts "n:d:lapc" OPTION2; do
                          case "$OPTION2" in 
                       
                       n)  
                          setup 
                    echo "Restore file or files"
                          RESTORES="$@"
                          for f in $RESTORES
                          do
                          f=`echo "$f" | sed 's/^[ \t]*//;s/[ \t]*$//'`
                          if [ -f ${f}.tar.gz ]
                          then
         tar -xzvf $BACKUP_FILES_DIRECTORY/$f.tar.gz -C $RESTORE_FILES_DIRECTORY
                          echo "SUCCESS"
                          sleep 3s
                         else
                         echo "File not found for $f...."
                         sleep 3s
                        exit
                        fi
                        done 
                           sleep 2s                         
                           ;;  

                       d)
                         setup
                             echo "Restore file name "$2" to dir "$4""
                          BACKUP_NAME="$2"
                           echo -e "$BACKUP_NAME"
                          RESTORE_FILES_DIRECTORY="$4"
                           echo -e "$RESTORE_FILES_DIRECTORY"
                           sleep 3s
                   restore_dir_check
                       cd $BACKUP_FILES_DIRECTORY
                   backup_name_check
                     tar -xzvf $BACKUP_NAME -C $RESTORE_FILES_DIRECTORY
                   if [[ $? == 0 ]]; then  echo "SUCCESS"
                   else echo "FAILURE"                   
                      sleep 3s
                  fi
                            ;;
 
Old 03-19-2013, 01:12 PM   #2
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Batistuta_g_2000 View Post
Simple backup/restore Case menu:

Will not work - I need options
-b -full
-b -error
or
-r -n xxxx xxxx xxxx xxx (as many as you like)
-r xxxxx -d xxxxxx (restore name and new directory)

Have the seconf -r xxxx -d xxxx working.........
..................................................
BUT, I cannot seem to find any way of getting the
-r -n [Array] to load into case below.

Please help, stuck for days now.


Code:
clear

while getopts "b:r:c:" OPTION; do
               case "$OPTION" in                        
                 b)
                     case "${OPTARG}" in 
                                                                                      
                        full)
                          backup_action 
                        ;;                         
                        error)
                          backup_action 
                        ;;
                      *) 
                           echo "Wrong choice"
                       ;;   
                      esac ;;                             
                 r)
                     while getopts "n:d:lapc" OPTION2; do
                          case "$OPTION2" in 
                       
                       n)  
                          setup 
                    echo "Restore file or files"
                          RESTORES="$@"
                          for f in $RESTORES
                          do
                          f=`echo "$f" | sed 's/^[ \t]*//;s/[ \t]*$//'`
                          if [ -f ${f}.tar.gz ]
                          then
         tar -xzvf $BACKUP_FILES_DIRECTORY/$f.tar.gz -C $RESTORE_FILES_DIRECTORY
                          echo "SUCCESS"
                          sleep 3s
                         else
                         echo "File not found for $f...."
                         sleep 3s
                        exit
                        fi
                        done 
                           sleep 2s                         
                           ;;  

                       d)
                         setup
                             echo "Restore file name "$2" to dir "$4""
                          BACKUP_NAME="$2"
                           echo -e "$BACKUP_NAME"
                          RESTORE_FILES_DIRECTORY="$4"
                           echo -e "$RESTORE_FILES_DIRECTORY"
                           sleep 3s
                   restore_dir_check
                       cd $BACKUP_FILES_DIRECTORY
                   backup_name_check
                     tar -xzvf $BACKUP_NAME -C $RESTORE_FILES_DIRECTORY
                   if [[ $? == 0 ]]; then  echo "SUCCESS"
                   else echo "FAILURE"                   
                      sleep 3s
                  fi
                            ;;

Got it........... had to split the array:

Code:
                       n) 
                          setup
                          echo "Restore file or files"
                 ACTION=RESTORE
                 echo "The Action of this script is "$ACTION""
         cd $BACKUP_FILES_DIRECTORY
           shift
          RESTORES="${@:3}"
          for f in $RESTORES
           do
             f=`echo "$f" | sed 's/^[ \t]*//;s/[ \t]*$//'`
             if [ -f ${f}.tar.gz ]
              then
            tar -xzvf $BACKUP_FILES_DIRECTORY/$f.tar.gz -C $RESTORE_FILES_DIRECTORY
            echo "SUCCESS"
           sleep 3s
 
Old 03-20-2013, 07:40 AM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
I don't think it's advisable to try to use two getopts commands together, at least not directly. There's only one global $OPTIND variable available in the environment, and they'll both try to use it at the same time.

You could probably put the second one inside a function though, with a separate, locally-scoped $OPTIND setting. Just pass the necessary $OPTARG value to it for processing.

Also, this:

Code:
RESTORES="${@:3}"
          for f in $RESTORES
           do
             f=`echo "$f" | sed 's/^[ \t]*//;s/[ \t]*$//'`
             if [ -f ${f}.tar.gz ]
'@' likewise will refer to the full script input parameters, unless you use a function or some other way to limit it to the option you want.

Next, you're simply moving the just-split array of items straight back into another scalar variable. The only reason it works at all is because you failed to quote the $RESTORES, so the shell re-word-splits it again. Either use the split array directly in the loop, or transfer it into another array and use that.

Third, once a string is inside a variable, the shell's built-in string manipulations are generally to be preferred over running them through sed or cut or whatever. And see here for how to handle this particular need.

When in bash use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

I've probably also already mentioned before that: $(..) is highly recommended over `..`. There's no reason at all to use backticks anymore.

And please lose the extra curly braces around your variables. They do nothing to help in most cases, and only clutter up the code.

Code:
for f in "${@:3}"; do
    read -rd f <<<"$f"
    if [[ -f $f.tar.gz ]]; then
Of course the above assumes you've moved the code into a function and passed the string onto it, so that the '$@' is referring to the correct thing. I'd still recommend using a simple array split or something instead.

Last edited by David the H.; 03-20-2013 at 07:43 AM. Reason: minor code fix
 
Old 03-22-2013, 03:57 AM   #4
Batistuta_g_2000
Member
 
Registered: Oct 2011
Posts: 85
Blog Entries: 1

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by David the H. View Post
I don't think it's advisable to try to use two getopts commands together, at least not directly. There's only one global $OPTIND variable available in the environment, and they'll both try to use it at the same time.

You could probably put the second one inside a function though, with a separate, locally-scoped $OPTIND setting. Just pass the necessary $OPTARG value to it for processing.

Also, this:

Code:
RESTORES="${@:3}"
          for f in $RESTORES
           do
             f=`echo "$f" | sed 's/^[ \t]*//;s/[ \t]*$//'`
             if [ -f ${f}.tar.gz ]
'@' likewise will refer to the full script input parameters, unless you use a function or some other way to limit it to the option you want.

Next, you're simply moving the just-split array of items straight back into another scalar variable. The only reason it works at all is because you failed to quote the $RESTORES, so the shell re-word-splits it again. Either use the split array directly in the loop, or transfer it into another array and use that.

Third, once a string is inside a variable, the shell's built-in string manipulations are generally to be preferred over running them through sed or cut or whatever. And see here for how to handle this particular need.

When in bash use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression

I've probably also already mentioned before that: $(..) is highly recommended over `..`. There's no reason at all to use backticks anymore.

And please lose the extra curly braces around your variables. They do nothing to help in most cases, and only clutter up the code.

Code:
for f in "${@:3}"; do
    read -rd f <<<"$f"
    if [[ -f $f.tar.gz ]]; then
Of course the above assumes you've moved the code into a function and passed the string onto it, so that the '$@' is referring to the correct thing. I'd still recommend using a simple array split or something instead.

Great help, thank you so much. Just one thing though how can I pass optargs from case statements to functions:
Code:
ie getopts case1 "abc:d:"

a)
function one
b) 
function two
c)
 getopts case2 "f:g:"
           f)
             option1 "c" argument and "f" argument/s passed to function three
          g)
             option1 "c" argument and "g" argument/s passed to function four
d)
 getopts case3 "hi:"
          h)
           ##### ./script -d -h
          i)
            option1 "d" argument and "i" argument/s passed to function five
So how on earth can this be done?

Please and thanks a million for help already?

Last edited by Batistuta_g_2000; 03-22-2013 at 04:02 AM.
 
Old 03-26-2013, 12:31 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Functions are like mini-scripts. You pass values to them just as you would with any other command, through their parameters.

Code:
splitargs() {
    local -a arglist
    IFS=: read -a arglist <<<"$@"
    printf '%s\n' "${arglist[@]}"
}

variable='foo:bar:baz:foo bar baz'

splitargs "$variable"
Functions can also read (and modify) global variables from their parent shell, since they exist as part of the shell's environment.
 
  


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
LXer: Redo- A simple live CD to Backup-Restore your computer LXer Syndicated Linux News 0 01-09-2012 03:20 PM
[SOLVED] lost grub menu after windows installation & forgot to backup menu.lst jesman1 Linux - Kernel 4 08-13-2010 12:57 AM
how do i restore lower case filenames in a source code package? unclejed613 Linux - Software 6 01-02-2010 10:33 AM
Weird, file names changing case after restore... s2cuts Linux - General 2 03-03-2007 02:39 PM
the simple way to backup and restore HDD prince amir Linux - Newbie 1 01-09-2006 11:07 AM

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

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