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 and shell scripting languages 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-02-2007 01:09 AM
precedence of stderr and stdout new2ss Shell Programming and Scripting 1 06-09-2006 12:03 AM
help in getopts problems Shell Programming and Scripting 1 05-05-2006 12:07 AM
getopts yerra Shell Programming and Scripting 5 03-26-2005 10:43 AM
EOF & precedence of != alan High Level Programming 4 04-29-2004 03:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 07-13-2006
jack1981 jack1981 is offline
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
  #2 (permalink)  
Old 07-17-2006
aigles's Avatar
aigles aigles is online now Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,427
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.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0