looping while using getopts


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting looping while using getopts
# 1  
Old 02-20-2012
looping while using getopts

so when i use rm file -i vv bb i can delete the first file vv but i cant seem to delete the second file bb as i tried to use shift on it, but it seems that it delete the first one and stops it without looping it to the next file. what is wrong with my coding ?

Code:
#!/bin/bash

function removei () {



clear

if [[ -d $file ]]; # -d allows to check if the value is a directory
     then
                                echo -e "are you sure you want to remove this directory (y/n)"
                                        read ANSWER


                case "$ANSWER" in 

                                        [Yy]) echo "the directory $file has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift
                                                    ;;

                                        [Nn]) echo -e "exiting"
                                                       exit
                                                       ;;


                                        *) echo "Answer y or n"
                                                ;;

                 esac

elif [[ -f $file ]]; # -f allows to check if the value is a file
        then
                               echo "are you sure you want to remove the file (y/n)"

                                   read  ANSWER1
case "$ANSWER1" in


                                        [Yy]) echo "the file $file  has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift
                                              echo "the file $file  has been deleted"
                                                    mv $file $HOME/deleted
                                                    shift

                                                    ;;

                                        [Nn]) echo "exiting"
                                                    exit;;


                                        *) echo "Answer y or n";;

                esac

else
        echo "$file does not exist" 
fi


}

file=$2

while getopts "ivfrR:" option



do

case $option in

        i) removei;;

        v) removev;;

        f) removef;;

esac

shift 1

done

# 2  
Old 02-20-2012
The first thing I noticed was that you set file outside of your while loop, so it will always have the same value.

I'm not a heavy getopts users, but it seems that shifting in the getopts loop is wrong. My impression is that the way getopts is intended to be used is to 'gather' information from command line options, and parse positional parameters outside of that loop. Something like this:

Code:
function removei
{
    echo "removei: $f"
}

method="i"                # default method should the script be invoked without flags
while getopts "ivr" flag     # set method based on the command line options
do
    case $flag in
        i)  method=i;;
        r)  method=r;;
        v)  method=v;;
    esac
done

shift $(( ${OPTIND:-1} - 1 ))   # shift away 'used' flags and flag/data
while [[ -n $1 ]]   # until there are no more positional parameters
do
    f=$1              # assign file name each pass of the loop
    case $method in    # invoke method selected by -i|-v|-r above for current file
        i)  removei;;
        v)  echo "removev";;
        r)  echo "removev";;
    esac

    shift      # shift away last file/directory worked on 
done

exit


Last edited by agama; 02-20-2012 at 10:41 PM.. Reason: default
# 3  
Old 02-21-2012
ok the coding that you gave me works however, on the

Code:
function removei
{
    echo "removei: $f"
}

i wanted to add the validation bits that i showed on the first comment where it will check if its a file or directory and deletes it then go to the next file, however it still doesnt shift it when i put in the code of that, if you know what i mean.
# 4  
Old 02-21-2012
If you are trying to shift in the function, it's not going to do what you think as only one filename per call is being passed. The shift will need to take place in the second loop. If you are shifting in the second loop and it's not working it's difficult to say what is going on without seeing your script.
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. Shell Programming and Scripting

Getopts help

Hi All, I am writing a script to pass the getopts argument to the function which I have. But it as soon as I execute the script, the argument is taking it as blank. I tried using multiple way to check but its not working. Can someone please let me know what wrong in this code. function1()... (4 Replies)
Discussion started by: sidh_arth85
4 Replies

3. 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

4. Shell Programming and Scripting

? used in getopts

Suppose I have a code below . while getopts a: opt do case $opt in a) app_name="$OPTARG";; *) echo "$opt is an invalid option"; exit 1;; ?) echo "The value of $OPTARG is an invalid option"; exit 1;; esac done Could anyone please tell me in which case my... (1 Reply)
Discussion started by: maitree
1 Replies

5. Shell Programming and Scripting

getopts help

First off, I apologize for my lack of knowledge. I realize my problem will probably seem pretty basic to everyone, but I've been at this for several hours now and I've gotten nowhere. I would contact my professor, but it is too late for that. Anyway, I'm trying to write a function called... (1 Reply)
Discussion started by: Unknown50862
1 Replies

6. UNIX for Dummies Questions & Answers

Getopts

Hey, i need help with the use of getopts in shell script. tried reading a lot online, but found incomplete examples (maybe complete but cudn't make out). PLzz help...explain in deatil plzzz, i am a newbie:confused: (3 Replies)
Discussion started by: SasankaBITS
3 Replies

7. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

8. 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

9. Shell Programming and Scripting

getopts help

Hi i have part of the scripts below ,getopt for -h or ? not working for me. can anybody tell me if this sytax right or wrong. #!/usr/bin/ksh program=$(basename $0) ##################################################################################### function usageerr { RC=1 ... (3 Replies)
Discussion started by: GrepMe
3 Replies

10. Shell Programming and Scripting

help in getopts

hey need help with getopts again. i am using getopts to read my command line options and arguments. i can manage to do for options that have only one argument e.g srcipt_name -f 3 i am able to use getopts to do this but i am having problems two accept more than two agruments e.g.... (1 Reply)
Discussion started by: problems
1 Replies
Login or Register to Ask a Question