The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help in getopts chella Shell Programming and Scripting 4 11-01-2007 10:09 PM
precedence of stderr and stdout new2ss Shell Programming and Scripting 1 06-08-2006 08:03 PM
help in getopts problems Shell Programming and Scripting 1 05-04-2006 08:07 PM
getopts yerra Shell Programming and Scripting 5 03-26-2005 07:43 AM
EOF & precedence of != alan High Level Programming 4 04-29-2004 11:47 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-13-2006
Registered User
 

Join Date: Jul 2006
Posts: 42
setting precedence with getopts

Hi,

I am re-writing a script I wrote which emulated the "rm" command, in my orginal script I had problems with precedence, I did find a way round it by creating a seperate case statements which checked the options and performed the actions accordingly, does anyone know if I can use getopts better to help with precedence? and save on using the extra case statement.

So instead of this part of my code:

Code:
function delete() {
while :
do  case $OPTS in

      v|ivf|vf|ifv|vif) verbose $@
                      break
                      ;;
    fi|vfi|fvi|iv|vi|fiv) intVerbose $@
                      break
                      ;;
               f|fv|if) mv -f $@ $TRASH 2>/dev/null
                      break
                      ;;
                     i) int $@
                      break
                      ;;
                     r)mv $@ $TRASH 2>/dev/null
                      break
                      ;;
                     *)mv $@ $TRASH 2>/dev/null
                      break

esac
done

}


# GETOPTS

while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=
             ;;
             f) FLAG_F=f
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_I=i
             ;;
             *) errorInvalidOpt

      esac
done
shift `expr $OPTIND - 1`

OPTS=$FLAG_R$FLAG_F$FLAG_I$FLAG_V
I can manage it within getopts by just using this:

Code:
while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=r    #what can I add to my getopts to set the right flags
             ;;                   #  in order with rm's precedence
             f) FLAG_F=f        
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_I=i
             ;;
             *) errorInvalidOpt

      esac
done
shift `expr $OPTIND - 1`
I have ommitted the rest of my code but I am trying to emulate the "rm" and I am providing other functions to move the files to a trash folder rather than delete for good.

So, given this option argument: -fi it should not force but should interact but if I enter -if it should not interact but should force, how do I utilise getopts to allow for this withour the case statement in the top code?

Thanks

Jack
Reply With Quote
Forum Sponsor
  #2  
Old 07-17-2006
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,212
You can do somthiong like this :

Code:
while getopts :rRfvi o
do    case $o in
           r|R) FLAG_R=r    #what can I add to my getopts to set the right flags
             ;;                   #  in order with rm's precedence
             f) FLAG_F=f
                FLAG_I=        
             ;;
             v) FLAG_V=v
             ;;
             i) FLAG_I=i
                FLAG_F=
             ;;
             *) errorInvalidOpt

      esac
done
shift `expr $OPTIND - 1`
Jean-Pierre.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:15 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0