setting precedence with getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting setting precedence with getopts
# 1  
Old 07-13-2006
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  
Old 07-17-2006
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using getopts

Hi. Can somebody please show me an example of how to use getopts to assign a variable if it's been passed into the script but to set a default if no value has been passed in? And also how to handle a param with multiple values ... so a sub parse (can I use a function for this?)? Here's my code... (1 Reply)
Discussion started by: user052009
1 Replies

2. UNIX for Dummies Questions & Answers

Getopts

while getopts v OPTION do case $OPTION in v) echo "Hello" ;; *) exit 1;; esac done Suppose I have script tmp.sh Whose Signature is tmp.sh <fixed_argument> When I run the script with tmp.sh -v "file", it echoes a hello but, when I try the other way i.e, tmp.sh... (1 Reply)
Discussion started by: Devendra Hupri
1 Replies

3. Solaris

Is there a difference between setting a user as nologin and setting it as a role?

Trying to figure out the best method of security for oracle user accounts. In Solaris 10 they are set as regular users but have nologin set forcing the dev's to login as themselves and then su to the oracle users. In Solaris11 we have the option of making it a role because RBAC is enabled but... (1 Reply)
Discussion started by: os2mac
1 Replies

4. UNIX for Dummies Questions & Answers

What should be precedence of using awk, sed, head and tail in UNIX?

Hi All, I am new to unix. In this forum some days back, I have read something like below: 1) Do not use perl if awk can do your work. 2) Do not use awk if sed can do your work. . . . I do not re-collect the whole thing. I think it is good to know the precedence of using these... (2 Replies)
Discussion started by: Prathmesh
2 Replies

5. Shell Programming and Scripting

Precedence in operators issue

Hello, I am trying to write a small acript to change directory to $HOME depending on the user logged in. However when i provide this command say, ABC_USER=myself cd ~${ABC_USER} i am getting the following error, ksh: ~myself: not found I know i am doing something really silly but... (4 Replies)
Discussion started by: arvindspr06
4 Replies

6. Programming

Makefile -> pc precedence over c

Hi All, I have created a common makefile that compiles both pc and c files. i have created the dependency between the files as .pc.o: ----------- .c.o: ----------- I will be deleting the .c files created from the .pc files, once the object file is created. ( better storage... (7 Replies)
Discussion started by: quintet
7 Replies

7. Shell Programming and Scripting

Help in getopts

Hi, My script will take a input file as a parameter(which is not mandatory) and also an option. ksh my_script.sh <inputfile> The option -n I have given is no way related to the input file. Now the problem here is when i execute the script specifying the input file and the option(the way... (4 Replies)
Discussion started by: chella
4 Replies

8. Shell Programming and Scripting

precedence of stderr and stdout

#!/usr/bin/perl open(STDOUT, ">>$Textfile") open(STDERR, ">>$Textfile") print "program running\n"; $final = join("+", $initial,$final) #5 close (STDOUT); close (STDERR);Hi all, above is my perl code. Notice i have captured the stdout and stderr to the same textfile. my code is expected to... (1 Reply)
Discussion started by: new2ss
1 Replies

9. Shell Programming and Scripting

getopts

I have a script which fires a command based on certain parameters. I am posting the code below.. The options needs be given such that -u option goes along with -d and -s, -f goes with -d and -t goes with -s and -d. 1) How do I ensure that user misses any of the option then he should be... (5 Replies)
Discussion started by: yerra
5 Replies

10. Programming

EOF & precedence of !=

Gurus, I am teaching myself C and have a question. I wrote a small prog that reads characters as entered at the prompt and checks the value for EOF. Unless I am 100% wrong, the value will be '1' until getchar() has anything to read in my stream. /* PROG 1 */ #include <stdio.h> ... (4 Replies)
Discussion started by: alan
4 Replies
Login or Register to Ask a Question